普通的POJO只要标注@Configuration注解,就可以为Spring容器提供Bean定义的信息了,每个标注了@Bean的类方法都相当于提供一个Bean的定义信息。

基于Java类的配置方法和基于XML或基于注解的配置方式相比,前者通过代码的方式更加灵活地实现Bean的实例化及Bean之间的装配,但后面两者都是通过配置声明的方式,在灵活性上要稍逊一些,但是配置上要更简单一些。

 
UserDao类:

package com.ioc.cha4_11;
public class UserDao {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "UserDao{" +
"name='" + name + '\t' +
'}';
}
public UserDao() {
System.out.println("userDao");
}
}
logDao类:

package com.ioc.cha4_11;
public class LogDao {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "LogDao{" +
"name='" + name + '\t' +
'}';
}
public LogDao() {
System.out.println("LogDao");
}
}

logonService类:

package com.ioc.cha4_11;
public class LogonService {
private LogDao logDao;
private UserDao userDao;
public LogDao getLogDao() {
return logDao;
}
public void setLogDao(LogDao logDao) {
this.logDao = logDao;
}
public UserDao getUserDao() {
return userDao;
}
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
} public void printHelllo(){
System.out.println("hello!");
}
}

AppConf类:

package com.ioc.cha4_11;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
//将一个POJO标注为定义Bean的配置类
@Configuration
public class AppConf {
//以下两个方法定义了两个Bean,并提供了Bean的实例化逻辑
@Bean
public UserDao userDao() {
return new UserDao();
}
@Bean
public LogDao logDao() {
return new LogDao();
}
//定义了logonService的Bean
@Bean
public LogonService logonService() {
LogonService logonService = new LogonService();
//将前面定义的Bean输入到LogonService Bean中
logonService.setLogDao(logDao());
logonService.setUserDao(userDao());
return logonService;
}
}

beans1.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"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="userDao" class="com.ioc.cha4_11.UserDao"/>
<bean id="logDao" class="com.ioc.cha4_11.LogDao"/>
<bean id="logonService" class="com.ioc.cha4_11.LogonService"
p:logDao-ref="logDao" p:userDao-ref="userDao"/>
</beans>

测试类:

package com.ioc.cha4_11;
/**
* Created by gao on 16-3-25.
*/
public class Test {
public static void main(String[] args) {
AppConf ac = new AppConf();
LogonService ls = ac.logonService();
ls.printHelllo();
}
}
输出结果:
LogDao
userDao
hello!
 
 
DaoConfig类:
package com.ioc.cha4_11;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
@Configuration
public class DaoConfig { @Bean(name="userDao")
public UserDao userDao(){
return new UserDao();
}
//每次调用该方法都会返回一个新的LogDao Bean
@Scope("prototype")
@Bean
public LogDao logDao(){
return new LogDao();
}
}

ServiceConfig类:

package com.ioc.cha4_11;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
@Configuration
@Import(DaoConfig.class)
public class ServiceConfig {
//像普通Bean一样注入DaoConfig
@Autowired
private DaoConfig daoConfig;
@Bean
public LogonService logonService(){
LogonService logonService = new LogonService();
System.out.println(daoConfig.logDao() == daoConfig.logDao());
//像普通Bean一样,调用Bean相关的方法
logonService.setLogDao(daoConfig.logDao());
logonService.setUserDao(daoConfig.userDao());
return logonService;
}
}

LogonAppConfig类:

package com.ioc.cha4_11;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
@Configuration
@ImportResource("classpath:com\\ioc\\cha4_11\\beans3.xml")
public class LogonAppConfig { @Bean
@Autowired
public LogonService logonService(UserDao userDao,LogDao logDao){
LogonService logonService = new LogonService();
logonService.setUserDao(userDao);
logonService.setLogDao(logDao);
return logonService;
}
}

beans2.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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="com.ioc.cha4_11"
resource-pattern="AppConf.class" />
</beans>

beans3.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-3.0.xsd">
<bean id="userDao" class="com.ioc.cha4_11.UserDao"/>
<bean id="logDao" class="com.ioc.cha4_11.LogDao"/>
</beans>

测试类:

package com.ioc.conf;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class JavaConfigTest {
public static void main(String[] args) { //1.通过构造函数加载配置类
// ApplicationContext ctx = new AnnotationConfigApplicationContext(AppConf.class);
//2.通过编码方式注册配置类
// AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
// ctx.register(DaoConfig.class);
// ctx.register(ServiceConfig.class);
// ctx.refresh();
//3.通过XML组装@Configuration配置类所提供的配置信息
// ApplicationContext ctx = new ClassPathXmlApplicationContext("com/baobaotao/conf/beans2.xml");
//4.通过@Configuration组装XML配置所提供的配置信息
// ApplicationContext ctx = new AnnotationConfigApplicationContext(LogonAppConfig.class);
//5.@Configuration的配置类相互引用
ApplicationContext ctx = new AnnotationConfigApplicationContext(DaoConfig.class,ServiceConfig.class);
LogonService logonService = ctx.getBean(LogonService.class);
System.out.println((logonService.getLogDao() !=null));
logonService.printHelllo();
}
}
 
 
 
 
 
 
 

Spring IoC — 基于Java类的配置的更多相关文章

  1. Spring学习(13)--- 基于Java类的配置Bean 之 @Configuration & @Bean注解

    基于Java配置选项,可以编写大多数的Spring不用配置XML,但有几个基于Java的注释的帮助下解释.从Spring3.0开始支持使用java代码来代替XML来配置Spring,基于Java配置S ...

  2. Spring学习(16)--- 基于Java类的配置Bean 之 基于泛型的自动装配(spring4新增)

    例子: 定义泛型Store package javabased; public interface Store<T> { } 两个实现类StringStore,IntegerStore p ...

  3. Spring学习(15)--- 基于Java类的配置Bean 之 @Bean & @Scope 注解

    默认@Bean是单例的,但可以使用@Scope注解来覆盖此如下: @Configuration public class MyConfiguration { @Bean @Scope("pr ...

  4. Spring学习(14)--- 基于Java类的配置Bean 之 @ImportResource & @Value 注解

    学习如何使用@ImportResource 和 @Value 注解进行资源文件读取 例子: 先创建一个MyDriverManager类(模拟读取数据库配置信息) package com.beanann ...

  5. 《精通Spring4.X企业应用开发实战》读后感第五章(基于Java类的配置)

  6. 开涛spring3(12.4) - 零配置 之 12.4 基于Java类定义Bean配置元数据

    12.4  基于Java类定义Bean配置元数据 12.4.1  概述 基于Java类定义Bean配置元数据,其实就是通过Java类定义Spring配置元数据,且直接消除XML配置文件. 基于Java ...

  7. Spring Security基于Java配置

    Maven依赖 <dependencies> <!-- ... other dependency elements ... --> <dependency> < ...

  8. xml配置和基于java类的bean配置搭配使用

    如果同时使用了xml配置,和java类的bean配置(当然估计项目中一般不会这样), 在初始化容器指定资源文件的时候可能会比较麻烦 此时我们可以把基于java类的bean配置整合到xml中,或xml的 ...

  9. Spring使用注解开发及使用java类进行配置bean

    Spring使用注解开发 说明 在spring4之后,想要使用注解形式,必须得要引入aop的包 在配置文件当中,还得要引入一个context约束 <?xml version="1.0& ...

随机推荐

  1. C# 反射学习总结

    C#中的反射可以使得程序集和类型(类.结构.委托.接口和枚举)以及类型中的成员(方法.字段.属性.事件.参数.构造函数等)都成为变量在编程中动态调用.

  2. (转)《深入理解java虚拟机》学习笔记10——并发编程(二)

    Java的并发编程是依赖虚拟机内存模型的三个特性实现的: (1).原子性(Atomicity): 原子性是指不可再分的最小操作指令,即单条机器指令,原子性操作任意时刻只能有一个线程,因此是线程安全的. ...

  3. zip压缩包密码破解

    有一种破解方法叫做Known plaintext attack.市面上的密码破解软件几乎都带有这个功能.操作方法就是找到加密压缩包中的任意一个文件,用同样的压缩软件同样的压缩方式压缩成一个不加密的包, ...

  4. win7安装mysql

    转:http://blog.csdn.net/longyuhome/article/details/7913375 Win7系统安装MySQL5.5.21图解 大家都知道MySQL是一款中.小型关系型 ...

  5. javascript_22_for_js性感的v

    <script type="text/javascript"> window.onload=function(){ var aDiv=document.getEleme ...

  6. 【BZOJ】【1532】【POI2005】Kos-Dicing

    网络流/二分法 最大值最小……直接做不太好做的时候就可以用二分+判定来搞. 这题我们就也可以二分最大胜场v,那么怎么来判定呢?首先我们发现:每场比赛要么A赢,要么B赢,这一点跟二分图匹配非常类似,那么 ...

  7. SQL Server 之 DBCC

    --检查索引碎片情况 dbcc showconfig(tablename) 具体例子: --上图为碎片整理之前 ALTER INDEX ALL on Citation REBUILD --下图为碎片整 ...

  8. SQL Server 导数据 Oracle

    1. 使用Sql Server的企业管理器导入(推荐) 优点: 可以指定导入的表. 缺点: 转成Oracle时, 对应的数据类型要一个一个手动修改   2.使用ORACLE官方提供的Sql Devel ...

  9. Oracle中的 UPDATE FROM 解决方法

    转:http://www.cnblogs.com/JasonLiao/archive/2009/12/23/1630895.html Oracle中的 UPDATE FROM 解决方法 在表的更新操作 ...

  10. Winform 窗体的操作

    原文:http://www.cnblogs.com/Billy-rao/archive/2012/05/16/2503437.html 怎样能使winform窗体的大小固定住,不能调整其大小 窗体Fo ...