JavaEE互联网轻量级框架整合开发(书籍)阅读笔记(11):XML和Annotation装配Bean的混合使用(@ImportResource)
一、XML和Annotation装配Bean如何合理使用
引入第三方资源包中类的时候,建议使用XML配置,而使用自己编写的Java类的时候,推荐使用Annotation注解配置Bean。
二、关于注解@ImportResource的小例子
创建一个POJO类:UserBean.java
1 package com.xfwl.spring.annotation.xmlImport; 2 import org.springframework.beans.factory.annotation.Value; 3 import org.springframework.context.annotation.ComponentScan; 4 import org.springframework.stereotype.Component; 5 /** 6 * 测试注解:@Component装配Bean 7 * @author Jason 8 * 9 */ 10 //@Component("user") //注入user //@Component("user")或者@Component(value="user") 11 public class UserBean { 12 private String uname; 13 private String upwd; 14 public UserBean(){} 15 public UserBean(String uname,String upwd){ 16 this.uname=uname; 17 this.upwd=upwd; 18 } 19 public String getUname() { 20 return uname; 21 } 22 public void setUname(String uname) { 23 this.uname = uname; 24 } 25 public String getUpwd() { 26 return upwd; 27 } 28 public void setUpwd(String upwd) { 29 this.upwd = upwd; 30 } 31 @Override 32 public String toString() { 33 return "UserBean [uname=" + uname + ", upwd=" + upwd + "]"; 34 } 35 /**Bean生命周期测试**/ 36 public void init(){ 37 System.out.println("【"+this.getClass().getSimpleName()+"】执行自定义初始化方法!"); 38 } 39 public void mydestory(){ 40 System.out.println("【"+this.getClass().getSimpleName()+"】执行自定义销毁法!"); 41 } 42 }
创建一个xml配置文件:bean.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans 3 xmlns="http://www.springframework.org/schema/beans" 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 5 xsi:schemaLocation="http://www.springframework.org/schema/beans 6 http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"> 7 <!-- 依赖注入:bean --> 8 <bean id="user" class="com.xfwl.spring.annotation.xmlImport.UserBean"> 9 <property name="uname" value="xfww"/> 10 <property name="upwd" value="123456"/> 11 </bean> 12 </beans>
再创建一个xml配置文件:bean2.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"> <!-- 依赖注入:bean --> <bean id="user2" class="com.xfwl.spring.annotation.xmlImport.UserBean"> <property name="uname" value="xfww"/> <property name="upwd" value="123456"/> </bean> </beans>
创建一个管理类:ManagerScan.java
1 package com.xfwl.spring.annotation.xmlImport; 2 3 import org.springframework.beans.factory.BeanNameAware; 4 import org.springframework.beans.factory.DisposableBean; 5 import org.springframework.beans.factory.InitializingBean; 6 import org.springframework.beans.factory.annotation.Autowired; 7 import org.springframework.context.annotation.Bean; 8 import org.springframework.context.annotation.ComponentScan; 9 import org.springframework.context.annotation.Configuration; 10 import org.springframework.context.annotation.ImportResource; 11 @Configuration 12 @ImportResource({"classpath:com/xfwl/spring/annotation/xmlImport/bean.xml", 13 "classpath:com/xfwl/spring/annotation/xmlImport/bean2.xml"})//相对路径,@ImportResource注解中的配置项支持字符串数组 14 @ComponentScan(basePackages={"com.xfwl.spring.annotation.bean"}) //配置扫描包 15 public class ManagerScan{ 16 @Bean(name={"tom","jack"},initMethod="init",destroyMethod="mydestory") 17 public UserBean getUser(@Autowired UserBean user2){//此处使用user或者user2,经过测试都可以 18 return user2; 19 } 20 }
创建一个测试类:TestBean.java
1 package com.xfwl.spring.annotation.xmlImport; 2 import java.util.Properties; 3 4 import javax.sql.DataSource; 5 6 import org.apache.commons.dbcp2.BasicDataSourceFactory; 7 import org.springframework.context.ApplicationContext; 8 import org.springframework.context.annotation.AnnotationConfigApplicationContext; 9 import org.springframework.context.annotation.Bean; 10 import org.springframework.context.annotation.ComponentScan; 11 import org.springframework.stereotype.Component; 12 @Component("test") 13 public class TestBean { 14 public static void main(String[] args) { 15 //通过注解获取IoC容器对象 16 AnnotationConfigApplicationContext ctx=new AnnotationConfigApplicationContext(ManagerScan.class); 17 UserBean user=(UserBean) ctx.getBean("jack"); 18 System.out.println(user.toString()); 19 ctx.close(); 20 } 21 }
测试结果:
1 log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment). 2 log4j:WARN Please initialize the log4j system properly. 3 log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info. 4 【UserBean】执行自定义初始化方法! 5 UserBean [uname=xfww, upwd=123456] 6 【UserBean】执行自定义销毁法!
研究技术需要静下心来,一点一点地深究.......