写在前面:
众所周知,在JavaEE开发框架中,Spring框架是用的最多的,注解在框架的定位也就越来越明显了。说句玩笑话:能用一个注解解决的,绝不用一堆配置和代码解决;如果不能解决,那么来两个注解;(稳住,别喷…)
1.@Component是Spring定义的一个通用注解,可以注解任何bean。
2.@Scope定义bean的作用域,其默认作用域是”singleton”,除此之外还有prototype,request,session和global session。
案例:@[email protected]:
BeanAnnotation类:
@Scope @Component public class BeanAnnotation { public void say(String arg) { System.out.println("BeanAnnotation : " + arg); } public void myHashCode() { System.out.println("BeanAnnotation : " + this.hashCode()); } }
junit4测试类→TestBeanAnnotation类:
@RunWith(BlockJUnit4ClassRunner.class) public class TestBeanAnnotation extends UnitTestBase { public TestBeanAnnotation() { super("classpath*:spring-beanannotation.xml"); } @Test public void testSay() { BeanAnnotation bean = super.getBean("beanAnnotation"); bean.say("This is test."); } @Test public void testScpoe() { BeanAnnotation bean = super.getBean("beanAnnotation"); bean.myHashCode(); bean = super.getBean("beanAnnotation"); bean.myHashCode(); } }
Spring配置文件→spring-beanannotation.xml:
<context:component-scan base-package="com.beanannotation"></context:component-scan>
我们先从Spring配置文件分析,base-package="com.beanannotation"说明我们只处理这个包名下面的注解。
然后分析BeanAnnotation类,有一个say的方法。假设我们不清楚这是一个什么类型(注:Service或者DAO)的类,[email protected]
最后分析TestBeanAnnotation类,testSay方法里super.getBean("beanAnnotation")是从IOC的容器中取到这个bean,并调用bean的say方法。
提出问题的时间到了,当我们super.getBean的时候是通过bean的id从IOC容器中获取的,那么这个id是什么呢?[email protected],[email protected],[email protected](”bean”)的时候,在单元测试的时候就必须把super.getBean得到的id与之相对应才能测试成功。
[email protected],[email protected]nnotation类里面有一个myHashCode方法,可能大家有些疑惑,为什么要用this.hashCode()?[email protected],为了保证测试类的结果准确明了,所以采用哈希码值来判断是否为同一个对象。
3.@[email protected]@Controller是更具有针对性的注解。
PS:[email protected]注解哦:
[email protected],也就是我们常说的持久层。
[email protected],也就是服务层。
[email protected],也就是控制层(MVC)。
4.@Autowired理解为“传统”的setter方法,可以用在setter方法上,也可以用在构造器或者成员变量,能够进行Spring Bean的自动装配。
案例:@Autowired用法分析一:
Spring配置文件→spring-beanannotation.xml:
<context:component-scan base-package="com.beanannotation"></context:component-scan>
SimpleMovieLister类:
public class SimpleMovieLister { private MovieFinder movieFinder; @Autowired(required=false) public void setMovieFinder(MovieFinder movieFinder) { this.movieFinder = movieFinder; } }
在默认的情况下,如果找不到合适的bean将会导致autowiring失败抛出异常,[email protected],标记required=false来避免。但是,这不是一个必须的,如果找不到movieFinder的实例,是不会抛出异常的,只有在使用的时候发现movieFinder为null,在这种情况下,就要求我们在使用的时候,首先判断movieFinder是不是为null,如果是就会报空指针异常 。
值得注意的是,我们知道每个类可以有很多个构造器,[email protected],有且只能有一个构造器能够被标记为required=true(注:required的默认值为false)。
案例:@Autowired用法分析二:
BeanImplOne类:
@Order @Component public class BeanImplOne implements BeanInterface { }
BeanImplTwo类:
@Order @Component public class BeanImplTwo implements BeanInterface { }
BeanInterface类:
public interface BeanInterface { }
BeanInvoker类:
@Component public class BeanInvoker { @Autowired private List<BeanInterface> list; @Autowired private Map<String, BeanInterface> map; public void say() { if (null != list && 0 != list.size()) { for (BeanInterface bean : list) { System.out.println(bean.getClass().getName()); } } else { System.out.println(" list is null !"); } if (null != map && 0 != map.size()) { for (Map.Entry<String, BeanInterface> entry : map.entrySet()) { System.out.println(entry.getKey() + " " + entry.getValue().getClass().getName()); } } else { System.out.println("map is null !"); } } }
测试类TestInjection:
@RunWith(BlockJUnit4ClassRunner.class) public class TestInjection extends UnitTestBase { public TestInjection() { super("classpath:spring-beanannotation.xml"); } @Test public void testMultiBean() { BeanInvoker invoker = super.getBean("beanInvoker"); invoker.say(); } }
首先,我们清楚BeanImplOne类和BeanImplTwo类是实现了BeanInterface接口的,在BeanInvoker类里面我们定义了list和map,[email protected][email protected]类注入到list或者map中的呢?那么请看if循环语句和foreach循环打印,通过这个逻辑判断,如果能够打印出BeanImplOne类和BeanImplTwo类的路径名,就说明这样是可以的。如果有些小伙伴可能不信,[email protected],看结果怎么样。
测试类没有什么好说的,[email protected]?这里需要解释的就是,[email protected],比如1或者2,那么打印出来的路径名就会按顺序,[email protected]的功能。
5.@ImportResource注解引入一个资源,对应一个xml文件
6.@Value注解从资源文件中,取出它的key并赋值给当前类的成员变量
案例:@[email protected]:
MyDriverManager类:
public class MyDriverManager { public MyDriverManager(String url, String userName, String password) { System.out.println("url : " + url); System.out.println("userName: " + userName); System.out.println("password: " + password); } }
config.xml:
<context:property-placeholder location="classpath:/config.properties"/>
StoreConfig类:
@Configuration @ImportResource("classpath:config.xml") public class StoreConfig { @Value("${jdbc.url}") private String url; @Value("${jdbc.username}") private String username; @Value("${jdbc.password}") private String password; @Bean public MyDriverManager myDriverManager() { return new MyDriverManager(url, username, password); }
这个案例我们使用注解配置jdbc数据库的连接,首先创建一个内含构造器的MyDriverManager类,然后配置config.xml里面的资源文件路径,[email protected],最后配置StoreConfig类。(注意url、username、password也必须要和数据库的保持一致哦)
详解StoreConfig类:首先我们定义三个成员变量,[email protected],[email protected][email protected],[email protected]iguration注解是用来干嘛的呢?[email protected]?其实是这样的,@Component注解用于将所标注的类加载到 Spring 环境中,这时候是需要配置component-scan才能使用的,[email protected] 3.X后提供的注解,它用于取代XML来配置 Spring。
7.@Bean注解用来标识配置和初始化一个由SpringIOC容器管理的新对象的方法,类似XML中配置文件的<bean/>
ps:[email protected],那么有什么方式可以指定它的范围呢?[email protected]
8.@Scope注解,[email protected],proxyMode的属性是采用哪一种的单例方式(一种是基于接口的注解,一种是基于类的代理)
案例:@[email protected]:
@Bean @Scope(value ="session",proxyMode = "scopedProxyMode.TARGET_CLASS") public UserPreferences userPreferences(){ return new userPreferences(); } @Bean public service userService(){ UserService service =new SimpleUserService(); service.setUserPreferences(userPreferences); return service; }