汉莫拉比

(spring-第4回【IoC基础篇】)spring基于注解的配置

基于XML的bean属性配置:bean的定义信息与bean的实现类是分离的。

基于注解的配置:bean的定义信息是通过在bean实现类上标注注解实现。

也就是说,加了注解,相当于在XML中配置了,一样一样的。

 

一、举个栗子:

 1 package com.mesopotamia.annotation;
 2 
 3 import org.springframework.stereotype.Component;
 4 
 5 @Component
 6 public class Car {
 7     private String name;
 8     private String color;
 9     private double price;
10     public Car(){
11         name="保时捷";
12         color="黄色";
13     }
14     public String getName() {
15         return name;
16     }
17     public void setName(String name) {
18         this.name = name;
19     }
20     public String getColor() {
21         return color;
22     }
23     public void setColor(String color) {
24         this.color = color;
25     }
26     public double getPrice() {
27         return price;
28     }
29     public void setPrice(double price) {
30         this.price = price;
31     }
32 
33     public String toString(){
34         return "名字:"+name+" 颜色:"+color+" 价格:"+price;
35     }
36 }

 

第5行标注Component:spring看到这个属性标志,会自动将Car变成容器管理类,等同于在XML中这样配置:

1 <bean id="car" class="com.mesopotamia.annotation.Car"></bean>

 

二、spring启动之前的配置?

 

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 4     xmlns:context="http://www.springframework.org/schema/context"
 5     xsi:schemaLocation="http://www.springframework.org/schema/beans 
 6          http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
 7          http://www.springframework.org/schema/context
 8          http://www.springframework.org/schema/context/spring-context-3.0.xsd"
 9          >
10    <context:component-scan base-package="com.mesopotamia.annotation"/>
11 </beans>

 

如上,

使用空间:context,

使用context的规则:component-scan.

第10行的意义:扫描这个包中的所有类,并从注解信息中获取bean的基本信息(没加注解的不扫描也不实例化)。

 

三、如何启动?

 

按正常方式启动即可加载(自己复习spring容器的加载),例如:

 1 package com.mesopotamia.annotation;
 2 
 3 import org.springframework.context.ApplicationContext;
 4 import org.springframework.context.support.ClassPathXmlApplicationContext;
 5 
 6 public class Main {
 7     
 8     public static void main(String args[]){
 9         ApplicationContext ctx = new ClassPathXmlApplicationContext("com/mesopotamia/annotation/beans.xml");
10         Car car= ctx.getBean(Car.class);
11         System.out.println(car.toString());
12     }
13 
14 }

第10行就已经实例化Car了,并且实例化之前先跑了Car的构造函数给car的属性附了值(因为我给构造函数里专门写了赋值语句,没写的话,属性是没有赋值的,除非见了鬼)。

下面是打印信息,从下面第4行结果可以看到已经附了值。

1 2015-11-13 23:44:18,414  INFO [main] (AbstractApplicationContext.java:456) - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@e80a59: startup date [Fri Nov 13 23:44:18 CST 2015]; root of context hierarchy
2 2015-11-13 23:44:18,480  INFO [main] (XmlBeanDefinitionReader.java:315) - Loading XML bean definitions from class path resource [com/mesopotamia/annotation/beans.xml]
3 2015-11-13 23:44:18,728  INFO [main] (DefaultListableBeanFactory.java:555) - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@121ab80: defining beans [car,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor]; root of factory hierarchy
4 名字:保时捷 颜色:黄色 价格:0.0

 

四、context:component-scan的配置直接选了相应包里的全部class,有没有更多的过滤方式?

 

有。context:component-scan标签下面可以再加子标签,如:

1 <context:component-scan base-package="com.baobaotao">
2        <context:include-filter type="aspectj" expression="com.baobaotao.anno.*Plugin+"/>
3        <context:exclude-filter type="regex" expression="cn\.outofmemory\.spring\.[^.]+(Dao|Service)"/>
4</context:component-scan>

include-filter表示:包含哪些。type是类型,expression是表达式过滤。

exclude-filter表示:除去哪些。type是类型,expression是表达式过滤。

第二行表示:com.baobaotao.anno包下面所有以Plugin结尾的class。

第三行表示:除去所有以Dao或者Service结束的类。具体规则这里先不作讨论,你要觉得你行,那你自己先啃啃里面的肉(先去复习一下正则表达式)

其他过滤表达式举例:

 

五、除了Component外,其他的注解都有什么?

 

1、@Component 一般的bean类上面配置。

2、@Controller 对应表现层的Bean,也就是action。

3、@ Service 对应业务层bean。

4、@ Repository 对应持久层Bean。

上面的4种起到相同的效果,都是注入bean,注入bean的四大金刚,张龙赵虎王朝马汉,辅佐包黑炭。

 

5、@Autowired 成员变量或方法入参处标注,按类型匹配自动注入。

6、@Qualifier 按名称匹配方式注入。

7、@PostConstruct指定初始化方法。(相当于XML配置中的init-method)

8、@PreDestroy指定销毁方法。(相当于XML配置中的destroy-method)

9、@Scope 指定bean是prototype还是singleton。

 

六、上面这几个注解给个具体例子说明下?

 

 不想往下看的可以去陪女朋友了,因为重点已经讲完了。下面是一个简单登录界面的栗子,为了突出重点,关于spring web方面的机制暂时不讲,只讲有关注解的部分,如果你还没看过spring web应用部分也无妨,下面的例子只涉及注解注入部分:

XML配置文件如下:

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 4     xmlns:p="http://www.springframework.org/schema/p"
 5     xmlns:context="http://www.springframework.org/schema/context"
 6     xmlns:aop="http://www.springframework.org/schema/aop" 
 7     xmlns:tx="http://www.springframework.org/schema/tx"
 8     xsi:schemaLocation="http://www.springframework.org/schema/beans 
 9        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
10        http://www.springframework.org/schema/context 
11        http://www.springframework.org/schema/context/spring-context-3.0.xsd
12        http://www.springframework.org/schema/tx 
13        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
14        http://www.springframework.org/schema/aop
15        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
16     
17     <!-- 扫描类包,将标注Spring注解的类自动转化Bean,同时完成Bean的注入 -->
18     <context:component-scan base-package="com.baobaotao.dao"/>
19     <context:component-scan base-package="com.baobaotao.service"/>
20     <context:component-scan base-package="com.baobaotao.domain"/>
21     
22     <!-- 配置数据源 -->
23     <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
24         destroy-method="close" 
25         p:driverClassName="com.mysql.jdbc.Driver"
26         p:url="jdbc:mysql://localhost:3306/sampledb" 
27         p:username="root"
28         p:password="2009118293zjl" />
29 
30     <!-- 配置Jdbc模板  -->
31     <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"
32         p:dataSource-ref="dataSource" />
33         
34     <!-- 配置事务管理器 -->
35     <bean id="transactionManager"
36         class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
37         p:dataSource-ref="dataSource" />
38         
39     <!-- 通过AOP配置提供事务增强,让service包下所有Bean的所有方法拥有事务 -->
40     <aop:config proxy-target-class="true">
41         <aop:pointcut id="serviceMethod"
42             expression=" execution(* com.baobaotao.service..*(..))" />
43         <aop:advisor pointcut-ref="serviceMethod" advice-ref="txAdvice" />
44     </aop:config>
45     <tx:advice id="txAdvice" transaction-manager="transactionManager">
46         <tx:attributes>
47             <tx:method name="*" />
48         </tx:attributes>
49     </tx:advice>
50 </beans>

重点第18到20行:spring只扫描配置路径下所有带注解的地方来注入相关数据。其他行先不看,你想看我也不拦你。

WEB应用后台java分三层:control、service、dao层,简单讲一下,直接与jsp交互的就是control层,直接操作数据库的是dao层(持久层,数据库里的数据都是持久性的嘛),service层负责把dao层数据拿到,经过处理,返回给control层。control层拿到数据再交给jsp前台。(dao就是程序员,service就是老板,control就是客户,程序员做东西,客户使用,而老板是桥梁)

 

下面是control层:

 1 package com.baobaotao.web;
 2 
 3 import java.util.Date;
 4 
 5 import javax.annotation.PostConstruct;
 6 import javax.annotation.PreDestroy;
 7 import javax.servlet.http.HttpServletRequest;
 8 
 9 
10 import org.apache.commons.logging.Log;
11 import org.apache.commons.logging.LogFactory;
12 import org.springframework.beans.factory.annotation.Autowired;
13 import org.springframework.stereotype.Controller;
14 import org.springframework.web.bind.annotation.RequestMapping;
15 import org.springframework.web.servlet.ModelAndView;
16 
17 import com.baobaotao.domain.User;
18 import com.baobaotao.service.UserService;
19 
20 @Controller
21 public class LoginController{
22     Log log=LogFactory.getLog(LoginController.class);
23     
24     @Autowired(required=false)
25     private UserService userService;
26    
27     @RequestMapping(value = "/loginCheck.do")
28     public ModelAndView loginCheck(HttpServletRequest request,LoginCommand loginCommand){
29         boolean isValidUser = userService.hasMatchUser(loginCommand.getUserName(),loginCommand.getPassword());
30         if (!isValidUser) {
31             return new ModelAndView("login", "error", "用户名或密码错误。");
32         } else {
33             User user = userService.findUserByUserName(loginCommand
34                     .getUserName());
35             user.setLastIp(request.getLocalAddr());
36             user.setLastVisit(new Date());
37             userService.loginSuccess(user);
38             request.getSession().setAttribute("user", user);
39             
40             return new ModelAndView("main");
41         }
42     }
43     
44     @PostConstruct
45     public void init(){
46         log.info("本应用程序正式拉开帷幕。。。");
47     }
48     
49     @PreDestroy
50     public void destroy(){
51         log.info("本应用程序正式落幕,吓吓侬。。。");
52     }
53 }

这里用到了几个注解,

spring看到@Controller会如同看到@Component一样,转换成spring容器管理的bean。

spring看到@Autowired(required=false)会自动注入(实例化)下面的userService。(直接用就行,就不用你来new一个对象啦)。

spring看到@PostConstruct就会在实例化本Bean后接着执行这个方法;

spring看到@PreDestroy就会在销毁之前执行下面的方法;

你的眼神告诉我你不相信我。好的,咱们看日志:

tomcat启动的时候spring容器会自动加载,下面是tomcat启动的日志:

1 。。。。。。
 2 
 3 2015-11-15 21:24:31,673 DEBUG [main] (DefaultSingletonBeanRegistry.java:214) - Creating shared instance of singleton bean 'userDao'
 4 2015-11-15 21:24:31,674 DEBUG [main] (AbstractAutowireCapableBeanFactory.java:430) - Creating instance of bean 'userDao'
 5 2015-11-15 21:24:31,674 DEBUG [main] (AbstractBeanFactory.java:242) - Returning cached instance of singleton bean 'org.springframework.aop.support.DefaultBeanFactoryPointcutAdvisor#0'
 6 
 7 。。。。。。
 8 
 9 2015-11-15 21:24:31,676 DEBUG [main] (InjectionMetadata.java:82) - Processing injected method of bean 'userDao': AutowiredFieldElement for private org.springframework.jdbc.core.JdbcTemplate com.baobaotao.dao.UserDao.jdbcTemplate
10 2015-11-15 21:24:31,677 DEBUG [main] (AbstractBeanFactory.java:242) - Returning cached instance of singleton bean 'jdbcTemplate'
11 2015-11-15 21:24:31,677 DEBUG [main] (AutowiredAnnotationBeanPostProcessor.java:420) - Autowiring by type from bean name 'userDao' to bean named 'jdbcTemplate'
12 
13 。。。。。。
14 
15 2015-11-15 21:24:31,678 DEBUG [main] (AbstractAutowireCapableBeanFactory.java:458) - Finished creating instance of bean 'userDao'
16 
17 
18 。。。。。。
19 
20 2015-11-15 21:24:31,679 DEBUG [main] (DefaultSingletonBeanRegistry.java:214) - Creating shared instance of singleton bean 'userService'
21 2015-11-15 21:24:31,679 DEBUG [main] (AbstractAutowireCapableBeanFactory.java:430) - Creating instance of bean 'userService'
22 2015-11-15 21:24:31,679 DEBUG [main] (AbstractBeanFactory.java:242) - Returning cached instance of singleton bean 'org.springframework.aop.support.DefaultBeanFactoryPointcutAdvisor#0'
23 2015-11-15 21:24:31,680  INFO [main] (UserService.java:18) - 实例化了UserService...
24 
25 。。。。。。
26 
27 2015-11-15 21:24:31,683 DEBUG [main] (InjectionMetadata.java:82) - Processing injected method of bean 'userService': AutowiredFieldElement for private com.baobaotao.dao.UserDao com.baobaotao.service.UserService.userDao
28 2015-11-15 21:24:31,685 DEBUG [main] (AbstractBeanFactory.java:242) - Returning cached instance of singleton bean 'userDao'
29 2015-11-15 21:24:31,685 DEBUG [main] (AutowiredAnnotationBeanPostProcessor.java:420) - Autowiring by type from bean name 'userService' to bean named 'userDao'
30 2015-11-15 21:24:31,686 DEBUG [main] (InjectionMetadata.java:82) - Processing injected method of bean 'userService': AutowiredFieldElement for private com.baobaotao.dao.LoginLogDao com.baobaotao.service.UserService.loginLogDao
31 2015-11-15 21:24:31,686 DEBUG [main] (AbstractBeanFactory.java:242) - Returning cached instance of singleton bean 'loginLogDao'
32 2015-11-15 21:24:31,687 DEBUG [main] (AutowiredAnnotationBeanPostProcessor.java:420) - Autowiring by type from bean name 'userService' to bean named 'loginLogDao'
33 
34 。。。。。。
35 
36 2015-11-15 21:24:32,093  INFO [main] (UserService.java:18) - 实例化了UserService...
37 2015-11-15 21:24:32,094 DEBUG [main] (AbstractAutowireCapableBeanFactory.java:458) - Finished creating instance of bean 'userService'
38 2015-11-15 21:24:32,094 DEBUG [main] (DefaultSingletonBeanRegistry.java:214) - Creating shared instance of singleton bean 'loginLog'
39 
40 。。。。。。
41 
42 015-11-15 21:24:32,252  INFO [main] (DefaultListableBeanFactory.java:555) - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@c272bc: defining beans [loginController,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.web.servlet.view.InternalResourceViewResolver#0]; parent: org.springframework.beans.factory.support.DefaultListableBeanFactory@691dee
43 2015-11-15 21:24:32,252 DEBUG [main] (DefaultSingletonBeanRegistry.java:214) - Creating shared instance of singleton bean 'loginController'
44 2015-11-15 21:24:32,254 DEBUG [main] (AbstractAutowireCapableBeanFactory.java:430) - Creating instance of bean 'loginController'
45 2015-11-15 21:24:32,255  INFO [main] (LoginController.java:25) - 实例化了LoginController...
46 2015-11-15 21:24:32,264 DEBUG [main] (InitDestroyAnnotationBeanPostProcessor.java:200) - Found init method on class [com.baobaotao.web.LoginController]: public void com.baobaotao.web.LoginController.init()
47 2015-11-15 21:24:32,265 DEBUG [main] (InitDestroyAnnotationBeanPostProcessor.java:208) - Found destroy method on class [com.baobaotao.web.LoginController]: public void com.baobaotao.web.LoginController.destroy()
48 2015-11-15 21:24:32,265 DEBUG [main] (InitDestroyAnnotationBeanPostProcessor.java:251) - Found init method on class [com.baobaotao.web.LoginController]: org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleElement@316510
49 2015-11-15 21:24:32,265 DEBUG [main] (InitDestroyAnnotationBeanPostProcessor.java:259) - Found destroy method on class [com.baobaotao.web.LoginController]: org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleElement@5cd39ffa
50 2015-11-15 21:24:32,266 DEBUG [main] (InjectionMetadata.java:59) - Found injected element on class [com.baobaotao.web.LoginController]: AutowiredFieldElement for private com.baobaotao.service.UserService com.baobaotao.web.LoginController.userService
51 2015-11-15 21:24:32,267 DEBUG [main] (AbstractAutowireCapableBeanFactory.java:504) - Eagerly caching bean 'loginController' to allow for resolving potential circular references
52 2015-11-15 21:24:32,268 DEBUG [main] (InjectionMetadata.java:82) - Processing injected method of bean 'loginController': AutowiredFieldElement for private com.baobaotao.service.UserService com.baobaotao.web.LoginController.userService
53 2015-11-15 21:24:32,269 DEBUG [main] (AbstractBeanFactory.java:242) - Returning cached instance of singleton bean 'userService'
54 2015-11-15 21:24:32,270 DEBUG [main] (AutowiredAnnotationBeanPostProcessor.java:420) - Autowiring by type from bean name 'loginController' to bean named 'userService'
55 2015-11-15 21:24:32,271 DEBUG [main] (InitDestroyAnnotationBeanPostProcessor.java:291) - Invoking init method on bean 'loginController': public void com.baobaotao.web.LoginController.init()
56 2015-11-15 21:24:32,271  INFO [main] (LoginController.java:50) - 本应用程序正式拉开帷幕。。。
57 2015-11-15 21:24:32,271 DEBUG [main] (AbstractAutowireCapableBeanFactory.java:458) - Finished creating instance of bean 'loginController'
58 
59 。。。。。。

第44、45行是实例化LoginController,

第46/47行是根据@PostContruct和@PreDestroy发现了我们定义的初始化和销毁方法。

第53行,spring直接给了我们一个userService对象(因为它看到了@Autowired,required=false是说如果找不到对应的bean不报异常)。

第56行,spring看到@PostContruct执行了初始化方法。

当我把tomcat停掉,spring容器就会销毁,那么是不是要执行@PreDestroy下面的销毁方法?当然,你要相信科学,看停掉tomcat的日志:

。。。。。
5 2015-11-15 21:53:15,763 DEBUG [main] (InitDestroyAnnotationBeanPostProcessor.java:303) - Invoking destroy method on bean 'loginController': public void com.baobaotao.web.LoginController.destroy()
6 2015-11-15 21:53:15,763  INFO [main] (LoginController.java:55) - 本应用程序正式落幕,吓吓侬。。。
7 2015-11-15 21:53:15 org.apache.catalina.core.ApplicationContext log
8 信息: Closing Spring root WebApplicationContext
9 2015-11-15 21:53:15,764  INFO [main] (AbstractApplicationContext.java:1002) - Closing Root WebApplicationContext: startup date [Sun Nov 15 21:24:29 CST 2015]; root of context hierarchy

第六行果然执行了销毁方法。

请解释前面那个日志里第53行的Returning cached instance of singleton bean 'userService'。

是这样的,先看UserService的部分代码:

1 @Service
2 public class UserService {
3     private static Log log=LogFactory.getLog(UserService.class);
4     public UserService(){
5         log.info("实例化了UserService...");
6     }

我在这个类中注释了@Service,spring在启动的时候跟@Component、@Controller、@Repository一样,会自动把bean信息注入容器中(即把bean在spring容器中实例化),也就是说,当spring看到@Service时,会自动返回一个提前实例化好的UserService,而只要UserService上面不显式标注:@Scope("prototype"),那么默认UserService就是单例模式的。单例模式下,spring会把bean放到IoC容器的缓存池中,而把bean的引用返回给调用者。这就是Returning cached instance of singleton bean 'userService'这句话的意义。

好的,我把UserService的注解改成下面的方式:

1 @Service
2 @Scope("prototype")
3 public class UserService {
4     private static Log log=LogFactory.getLog(UserService.class);
5     public UserService(){
6         log.info("实例化了UserService...");
7     }
8     

那么启动tomcat打印日志如下:

 1 2015-11-15 22:11:09,723 DEBUG [main] (Cglib2AopProxy.java:755) - Method is declared on Advised interface: public abstract int org.springframework.aop.framework.Advised.indexOf(org.aopalliance.aop.Advice)
 2 2015-11-15 22:11:09,724 DEBUG [main] (Cglib2AopProxy.java:755) - Method is declared on Advised interface: public abstract int org.springframework.aop.framework.Advised.indexOf(org.springframework.aop.Advisor)
 3 2015-11-15 22:11:09,724 DEBUG [main] (Cglib2AopProxy.java:755) - Method is declared on Advised interface: public abstract java.lang.Class org.springframework.aop.TargetClassAware.getTargetClass()
 4 2015-11-15 22:11:09,760  INFO [main] (UserService.java:20) - 实例化了UserService...
 5 2015-11-15 22:11:09,760 DEBUG [main] (AbstractAutowireCapableBeanFactory.java:458) - Finished creating instance of bean 'userService'
 6 2015-11-15 22:11:09,761 DEBUG [main] (AutowiredAnnotationBeanPostProcessor.java:420) - Autowiring by type from bean name 'loginController' to bean named 'userService'
 7 2015-11-15 22:11:09,761 DEBUG [main] (InitDestroyAnnotationBeanPostProcessor.java:291) - Invoking init method on bean 'loginController': public void com.baobaotao.web.LoginController.init()
 8 2015-11-15 22:11:09,761  INFO [main] (LoginController.java:50) - 本应用程序正式拉开帷幕。。。
 9 2015-11-15 22:11:09,761 DEBUG [main] (AbstractAutowireCapableBeanFactory.java:458) - Finished creating instance of bean 'loginController'
10 2015-11-15 22:11:09,761 DEBUG [main] (AbstractBeanFactory.java:242) - Returning cached instance of singleton bean 'org.springframework.context.annotati

你会看到,在拉开帷幕之前,又实例化了一次UserService,这就是prototype的作用。

再看:

 1 @Controller
 2 public class LoginController{
 3     Log log=LogFactory.getLog(LoginController.class);
 4     
 5     public LoginController(){
 6         log.info("实例化了LoginController...");
 7     }
 8     
 9     @Autowired(required=false)
10     @Qualifier("service")
11     private UserService userService;
12    

第10行,我在userService上面加了 @Qualifier("service")的注释,这个表示,我引用的是名字为service的bean的实例,那么你要给UserService起个名字叫service:

1 @Service("service")
2 @Scope("prototype")
3 public class UserService {
4     private static Log log=LogFactory.getLog(UserService.class);
5     public UserService(){
6         log.info("实例化了UserService...");
7     }
8     

像第一行那样。(Component、Controller、Repository也是那样命名,如果是基于XML的注入如何命名?自己复习去。)

不加的话,系统找不到对应的bean,就会报错。

 

spring容器的三座大山:XML配置文件(或者注解配置、bean配置等),spring容器,bean类。不管是基于XML还是基于注解,无非就是为了把bean信息注入到spring容器中,南拳和北腿,各有千秋。下面是二者的对比:

本节内容就讲到这里。

 

 

 

吾生也有涯,而知也无涯。

            ——《庄子》 

posted on 2015-11-15 23:07  搬砖的小学生  阅读(3701)  评论(0编辑  收藏  举报