JavaEE互联网轻量级框架整合开发(书籍)阅读笔记(11):XML和Annotation装配Bean的混合使用(@ImportResource)

一、XML和Annotation装配Bean如何合理使用

  引入第三方资源包中类的时候,建议使用XML配置,而使用自己编写的Java类的时候,推荐使用Annotation注解配置Bean。

二、关于注解@ImportResource的小例子

创建一个POJO类:UserBean.java

 package com.xfwl.spring.annotation.xmlImport;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Component;
/**
* 测试注解:@Component装配Bean
* @author Jason
*
*/
//@Component("user") //注入user //@Component("user")或者@Component(value="user")
public class UserBean {
private String uname;
private String upwd;
public UserBean(){}
public UserBean(String uname,String upwd){
this.uname=uname;
this.upwd=upwd;
}
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public String getUpwd() {
return upwd;
}
public void setUpwd(String upwd) {
this.upwd = upwd;
}
@Override
public String toString() {
return "UserBean [uname=" + uname + ", upwd=" + upwd + "]";
}
/**Bean生命周期测试**/
public void init(){
System.out.println("【"+this.getClass().getSimpleName()+"】执行自定义初始化方法!");
}
public void mydestory(){
System.out.println("【"+this.getClass().getSimpleName()+"】执行自定义销毁法!");
}
}

创建一个xml配置文件:bean.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="user" class="com.xfwl.spring.annotation.xmlImport.UserBean">
<property name="uname" value="xfww"/>
<property name="upwd" value="123456"/>
</bean>
</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

 package com.xfwl.spring.annotation.xmlImport;

 import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
@Configuration
@ImportResource({"classpath:com/xfwl/spring/annotation/xmlImport/bean.xml",
"classpath:com/xfwl/spring/annotation/xmlImport/bean2.xml"})//相对路径,@ImportResource注解中的配置项支持字符串数组
@ComponentScan(basePackages={"com.xfwl.spring.annotation.bean"}) //配置扫描包
public class ManagerScan{
@Bean(name={"tom","jack"},initMethod="init",destroyMethod="mydestory")
public UserBean getUser(@Autowired UserBean user2){//此处使用user或者user2,经过测试都可以
return user2;
}
}

创建一个测试类:TestBean.java

 package com.xfwl.spring.annotation.xmlImport;
import java.util.Properties; import javax.sql.DataSource; import org.apache.commons.dbcp2.BasicDataSourceFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Component;
@Component("test")
public class TestBean {
public static void main(String[] args) {
//通过注解获取IoC容器对象
AnnotationConfigApplicationContext ctx=new AnnotationConfigApplicationContext(ManagerScan.class);
UserBean user=(UserBean) ctx.getBean("jack");
System.out.println(user.toString());
ctx.close();
}
}

测试结果:

 log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
【UserBean】执行自定义初始化方法!
UserBean [uname=xfww, upwd=123456]
【UserBean】执行自定义销毁法!
上一篇:JavaEE互联网轻量级框架整合开发(书籍)阅读笔记(12):XML配置自动扫描包,自动加载*.properties文件


下一篇:JavaEE互联网轻量级框架整合开发(书籍)阅读笔记(5):责任链模式、观察者模式