当前位置: 首页 > news >正文

营销最好的网站建设公司免费好用的crm软件

营销最好的网站建设公司,免费好用的crm软件,葫芦岛城乡建设委员会网站,wordpress修改个人头像一、环境与profile ​ 在3.1版本中,Spring引入了bean profile的功能。要使用profile,首先要将所有不同的bean定义整理到一个或者多个pofile之中,再将应用部署到每个环境时,确保对应的profile处于激活状态。 在Java配置中&#xf…

一、环境与profile

​ 在3.1版本中,Spring引入了bean profile的功能。要使用profile,首先要将所有不同的bean定义整理到一个或者多个pofile之中,再将应用部署到每个环境时,确保对应的profile处于激活状态。

  • 在Java配置中,可以使用@Profile注解来指定某个bean属于哪一个profile。

    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.Profile;
    import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
    import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;import javax.sql.DataSource;@Configuration
    public class DevelopmentProfileConfig {@Profile("dev")@Bean()public DataSource dataSource() {return new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.H2).addScript("classpath:schema.sql").addScript("classpath:test-data.sql").build();}
    }
    
  • 在XML中配置profile

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"xmlns:c="http://www.springframework.org/schema/c"xmlns:p="http://www.springframework.org/schema/p"xmlns:util="http://www.springframework.org/schema/util"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/utilhttp://www.springframework.org/schema/util/spring-util.xsd"profile="dev"></beans>
    

    或者

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"xmlns:c="http://www.springframework.org/schema/c"xmlns:p="http://www.springframework.org/schema/p"xmlns:util="http://www.springframework.org/schema/util"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/utilhttp://www.springframework.org/schema/util/spring-util.xsd"><beans profile="dev">......</beans><beans profile="prof">......</beans>
    </beans>
    

注意:

​ Spring确定那个profile处于激活状态,需要依赖两个独立的属性:

  • spring.profiles.active
  • spring.profiles.default

二、条件化的bean

@Conditional来源于spring-context包下的一个注解。Conditional中文是条件的意思,@Conditional注解它的作用是按照一定的条件进行判断,满足条件给容器注册bean。

三、处理自动装配的歧义性

1. 自动装配的歧义性

​ 例如,我们创建一个接口和三个实现该接口的类,并通过隐式的bean发现和自动装配机制进行注入bean。

// Dessert接口
public interface Dessert {void cook();
}// Cake类
@Component
public class Cake implements Dessert{private String name = "蛋糕";private String description = "水果";@Overridepublic void cook() {System.out.println(name + "加了一些" + description);}
}// Cookies类
@Component
public class Cookies implements Dessert{private String name = "饼干";private String description = "巧克力豆";@Overridepublic void cook() {System.out.println(name + "加了一些" + description);}
}// IceCream类
@Component
public class IceCream implements Dessert{private String name = "冰淇淋";private String description = "奥利奥碎屑";@Overridepublic void cook() {System.out.println(name + "加了一些" + description);}
}// 测试类
@Autowired
private Dessert dessert;@Test
public void compactDiscTest() {dessert.cook();
}

​ 此时,由于 CakeCookies IceCream 均为 Dessert,自动装配在此时会遇到歧义性,导致Spring无法做出选择,从而抛出org.springframework.beans.factory.UnsatisfiedDependencyException错误。

2. 进行处理

​ 当确实发生歧义性的时候,Spring提供了多种解决方案来解决遮掩的个问题。包括:

  • 将可选bean中的某一个设置为首选(primary)的bean;
  • 使用限定符(qualifier)来帮助Spring将可选的bean的方位缩小到只有一个bean。
1)@Primary
  1. 与@Component组合

    @Component
    @Primary
    public class Cookies implements Dessert{......
    }
    
  2. 与@Bean方法组合

    @Configuration
    public class DessertConfig {@Bean@Primarypublic Dessert dessert() {return new IceCream();}}
    
  3. <bean>元素中使用

    <bean id="iceCream"class="com.shiftycat.dessert.IceCream"primary="true">
    
2)@Qualifier

​ 在使用@Primary来表选首选bean时,如果标示了两个及以上的首选bean,那么该机制就会失效。为了解决这个问题,我们可以使用@Qualifier来规定限制条件以缩小满足要求的bean数量。

//方法1
@Component
@Qualifier
public class IceCream implements Dessert{......
}
//方法2
@Autowired
@Qualifier("iceCream")
private Dessert dessert;@Test
public void compactDiscTest() {dessert.cook();
}

当然,我们也可以创建自定义的限定符,例如:

@Component
@Qualifier("clod")
public class IceCream implements Dessert{......
}
@Autowired
@Qualifier("clod")
private Dessert dessert;@Test
public void compactDiscTest() {dessert.cook();
}

在Java配置显式定义bean的时候,@Qualifier也可以与@Bean注解一起使用。但是,此时,如果有两个bean都使用@Qualifier进行标记,也会出现错误。例如:

@Component
@Qualifier("cold")
public class IceCream implements Dessert{private String name = "冰淇淋";private String description = "奥利奥碎屑";@Overridepublic void cook() {System.out.println(name + "加了一些" + description);}
}@Component
@Qualifier("cold")
public class Popsicle implements Dessert{private String name = "棒冰";private String description = "巧克力豆";@Overridepublic void cook() {System.out.println(name + "加了一些" + description);}
}

同时,由于Java不允许在同一个条目上重复出现相同类型的多个注解,因此,使用多个@Qualifier注解编译器会提示错误。

// 编译错误
@Component
@Qualifier("cold")
@Qualifier("creamy")
public class IceCream implements Dessert{......
}

因此,我们可以使用自定义的限定符注解,从而可以更便捷地进行限定。

// 自定义限定注解
@Target({ElementType.TYPE, ElementType.CONSTRUCTOR,ElementType.FIELD, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Qualifier
public @interface Cold {
}@Target({ElementType.TYPE, ElementType.CONSTRUCTOR,ElementType.FIELD, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Qualifier
public @interface Creamy {
}@Target({ElementType.TYPE, ElementType.CONSTRUCTOR,ElementType.FIELD, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Qualifier
public @interface Fruity {
}
@Component
@Cold
@Fruity
public class Popsicle implements Dessert{......
}@Component
@Cold
@Creamy
public class IceCream implements Dessert{......
}
@Autowired
@Cold
@Fruity
private Dessert dessert;@Test
public void compactDiscTest() {dessert.cook();
}

四、bean的作用域

​ 在默认情况下,Spring应用上下文中所有的bean都是以单例(singleton)的形式创建的。而Spring定义了多种作用域,可以基于这些作用域创建bean,包括:

  • 单例(singleton):在整个应用中,只创建bean的一个实例。
  • 原型(prototype):每次注入或者通过Spring应用上下文获取的时候,都会创建一个新的bean实例。
  • 会话(Session):在Web应用中,为每个会话创建一个bean实例。
  • 请求(Request)在Web应用中,为每个请求创建一个bean实例。

bean的作用域可以使用@Scope或者<bean>元素中的scope属性进行设置。

@Component
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class Cake implements Dessert{......
}
<bean id="cake"class="com.shiftycat.dessert.Cake"scope="prototype">

在Web应用中,例如有一个bean代表用户的购物车,此时它的作用域一定是会话作用域。

《Spring实战(第4版)》

http://www.khdw.cn/news/24863.html

相关文章:

  • 深圳哪家公司需要网站建设的百度怎么推广
  • 做网站怎么挣钱全网seo
  • 用微信小程序怎么做网站制作公司网站大概多少钱
  • 哪个网站能找到做夜场的女孩百度小程序优化
  • 做网站属于什么学科西安百度竞价开户
  • 网站空间买什么的好双桥seo排名优化培训
  • 网站后台如何添加视频九江seo公司
  • 口腔网站模板网络游戏排行榜百度风云榜
  • 手机网站开发学习浏览器网站进入口
  • 网站需要多大的空间互联网广告营销
  • 国外网站翻墙怎么做网络推广外包哪个公司做的比较好
  • wordpress打开文章很慢页面优化的方法有哪些
  • 做网站如何被收录二级网站怎么做
  • 360安全网站怎么做号码认证百度seoo优化软件
  • php网站开发入门到精通教程网络营销服务企业有哪些
  • 大连住房和建设局网站郑州seo线上推广系统
  • sql与网站开发免费大数据分析网站
  • 做网站的图片素材google谷歌搜索
  • 烟台做网站推广的公司哪家好seochinazcom
  • 口碑好的网站建设吉林网络公司
  • 建设主管部门官方网站seo营销网站的设计标准
  • 餐饮网站建设方案2022最新永久地域网名
  • 安卓开发简单网站开发代码下载安卓排名优化
  • 企业品牌网站建设我们的优势百度推广app下载安卓版
  • 一个营业执照可以做几个网站网站seo诊断分析
  • 张家港做外贸网站信阳seo
  • 代理记账公司收费价格seo优化网站优化
  • 做网站需要注册商标多少类手游推广个人合作平台
  • 惠州市网站建设个人一个新品牌怎样营销推广
  • 山东定制网页建站手游推广渠道和推广方式