Spring一些新特性
- Spring profile : 让bean活起来
- 条件化的bean声明:过滤bean
- 自动装配与歧义性: 请给我一个精准的bean
- bean的作用域: 正确行使的权利
- SpringEL表达式: Spring自己的语言
1.自动装配与歧义性
用注解@Primary来确定优先考虑, 用@Qualifier明确具体Bean
以水果为例定义Fruit.java 接口,三个实现类,Peach.java Banana.java Apple.java
@Primary注解使用
package learn.chapter3;
public interface Fruit {
void favorFruit();
}
package learn.chapter3;
public class Peach implements Fruit{
public void favorFruit() {
System.out.println("我喜欢吃桃子");
}
}
package learn.chapter3;
public class Banana implements Fruit {
public void favorFruit() {
System.out.println("我喜欢吃香蕉");
}
}
package learn.chapter3;
public class Apple implements Fruit{
public void favorFruit() {
System.out.println("我喜欢吃苹果");
}
}
注入javaConfig类
package learn.chapter3.javaConfig;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.Profile;
import learn.chapter3.Apple;
import learn.chapter3.Banana;
import learn.chapter3.Fruit;
import learn.chapter3.Peach;
@Configuration
public class FruitConfig {
@Bean
@Primary
public Fruit getApple(){
return new Apple();
}
@Bean
public Fruit getPeach(){
return new Peach();
}
@Bean
public Fruit getBanana(){
return new Banana();
}
}
测试类:
package learn.chapter2;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import learn.chapter3.Fruit;
import learn.chapter3.javaConfig.FruitConfig;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes={FruitConfig.class})
public class UniqueTest {
@Autowired
private Fruit fruit;
@Test
public void favor(){
fruit.favorFruit();
}
}
总结:比如说优先喜欢苹果,就在注入苹果的上写上 @Primary注解,这就可以唯一区别其他水果,如果出现多个这就会报错,因为区别不开了。这个时候就可以用@Qulifier了
测试类:
package learn.chapter2;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import learn.chapter3.Fruit;
import learn.chapter3.javaConfig.FruitConfig;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes={FruitConfig.class})
public class UniqueTest {
@Autowired
@Qualifier("getPeach")
private Fruit fruit;
@Test
public void favor(){
fruit.favorFruit();
}
}
结果是注入桃子
@Qulifier的优先级高于@Primary2.bean的作用域
Spring应用上下文所有的bean 都是单例模式,每个类实例化一个对象
Spring定义了多种作用域,可以基于这些作用域创建bean
- 单例(Singleton):整个应用中,只创建Bean的一个实例
- 原型(Prototype):每次注入或者通过Spring应用上下文获取的时候,都会创建一个新的实例
- 会话(Session):在WEB应用中,为每个会话创建一个bean实例
- 请求(Request):在web应用中,为每个请求创建一个Bean 实例
注解:@Scope
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE) 或 @Scope(“prototype”)
用xml配置
<bean id="fruit" class="learn.chapter2.Apple" scope="prototype" />
2.1使用会话作用域
@Component
@Scope(value = WebApplicationContext.SCOPE_SESSION,
proxyMode = ScopedProxyMode.INTERFACES)
public ShoppingCart cart() {..}
这就是一个购物车,如果定义成单例,所有用户将会使用同一个购物车,如果设置为原生,每个地方的添加的商品将不能合并在一起,这是需要session类型的作用域,给会话有关,这里用到代理模式, 在启动时候这个session对象并没有注入到对应服务类,这时候委托一个类假设实现这个功能,当真正有session时候,通过委托去调用真实的购物车对象。
ScopedProxyMode.INTERFACES 代表是代理接口
如果ShoppingCart是具体的类,这时候就用CGLib来生成基于类的代理 这时候的设置就是ScopedProxyMode.TARGET_CLASS
在XML中声明作用域代理
<bean id="cart" class="com.myapp.ShoppingCart" scope="session">
<aop:scoped-proxy/>
</bean>
如果基于具体的类
<bean id="cart" class="com.myapp.ShoppingCart" scope="session">
<aop:scoped-proxy proxy-target-class="false"/>
</bean>
用AOP要在头部添加命名空间:
xmlns:aop = "http://www.springframework.org/schema/aop"
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
总结:@Qualifier 和 @Primary 来确定唯一bean,bean的作用域不同有不同的作用。