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】执行自定义销毁法!

 

转载于:https://www.cnblogs.com/newwind/p/9300662.html

Java EE轻量级框架整合开发扫描基包是指在Java EE开发中,使用轻量级框架(如Spring Framework、Hibernate、MyBatis等)时,对应用程序中所使用的基础包(通常是模型类所在的包)进行自动扫描,以便框架能够自动发现并加载这些类,从而简化配置和管理。 在Spring Framework中,这种自动扫描通常通过注解来实现,比如使用`@ComponentScan`注解或者在配置类中定义`ComponentScan` Bean。这样Spring容器在启动时会遍历指定的包及其子包,查找带有特定注解(如`@Component`、`@Service`、`@Repository`、`@Controller`等)的类,并将它们注册为Spring管理的Bean。 实现扫描基包的步骤通常包括: 1. 添加扫描注解:在Spring的配置类(带有`@Configuration`注解)上添加`@ComponentScan`注解,并指定要扫描的基包路径。 ```java @Configuration @ComponentScan(basePackages = {"com.example.project"}) public class AppConfig { } ``` 2. 使用注解标识组件:在各个类上使用相应的Spring注解,如`@Service`在服务层、`@Repository`在数据访问层、`@Controller`在控制器层等。 ```java @Service public class MyService { } ``` 3. 启动Spring容器:通过Spring的上下文启动类或者使用注解`@SpringBootApplication`(在Spring Boot项目中)来启动Spring应用程序上下文。 ```java public class Application { public static void main(String[] args) { SpringApplication.run(AppConfig.class, args); } } ``` 使用扫描基包的方式,可以大大减少XML配置或Java配置中显式声明Bean的工作量,使代码更加简洁,并且有利于维护和扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值