普通的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. 将开始我的WebForm控件开发之旅

    时间总是过得很快,一转眼三个月就过去了,三个月内发生了很多的事.因为学校的学习,离开了我入门WPF的公司:开发了第一个外包项目,做的是WebForm的:而且了马上要毕业了,毕业后的公司应该是专门用We ...

  2. JAVA面试题集之基础知识

                           JAVA面试题集之基础知识 基础知识:  1.C 或Java中的异常处理机制的简单原理和应用. 当JAVA程序违反了JAVA的语义规则时,JAVA虚拟机就 ...

  3. MySQL批量杀进程

    OS: CentOS 6.3 DB:5.5.14 mysql> show processlist;+----+------+-----------+--------------------+-- ...

  4. _cpluscplus

    _cpluscplus是c++中的定义,而c中没有该定义 1.用来判定代码是c类型还是c++类型 2._cplusplus的类型是"long int",值为199711L int ...

  5. cocos2dx中的CCRect的用法

    CCRect是cocos2dx中的一块矩形区域 常用于碰撞检测及渲染时限定纹理的大小 CCRect rect=spr.boundingBox(); CCRect rect=CCRect(0,0,spr ...

  6. Netty4.x中文教程系列(四) 对象传输

    Netty4.x中文教程系列(四)  对象传输 我们在使用netty的过程中肯定会遇到传输对象的情况,Netty4通过ObjectEncoder和ObjectDecoder来支持. 首先我们定义一个U ...

  7. Codeforces Round #348 (VK Cup 2016 Round 2, Div. 2 Edition) D. Little Artem and Dance

    题目链接: http://codeforces.com/contest/669/problem/D 题意: 给你一个初始序列:1,2,3,...,n. 现在有两种操作: 1.循环左移,循环右移. 2. ...

  8. SVN提交错误:working copy is not up-to-date解决方法

    我在项目中删了2个jar,然后SVN提交,一直提交不成功 svn在提交时报错如下图: working copy is not up-to-date svn:commit failed(details ...

  9. 求出1~13的整数中1出现的次数,并算出100~1300的整数中1出现的次数?为此他特别数了一下1~13中包含1的数字有1、10、11、12、13因此共出现6次,但是对于后面问题他就没辙了。ACMer希望你们帮帮他,并把问题更加普遍化,可以很快的求出任意非负整数区间中1出现的次数。

    // test20.cpp : 定义控制台应用程序的入口点. // include "stdafx.h" include include include include inclu ...

  10. 平常写css网页制作时最实用的九条CSS技巧

    一.使用css缩写 使用缩写可以帮助减少你CSS文件的大小,更加容易阅读.css缩写的主要规则请参看<css基本语法>. 二.明确定义单位,除非值为0 忘记定义尺寸的单位是CSS新手普遍的 ...