Spring IoC — 基于Java类的配置

时间:2021-09-16 07:01:30

普通的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 之 &commat;Configuration &amp&semi; &commat;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 之 &commat;Bean &amp&semi; &commat;Scope 注解

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

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

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

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

  6. 开涛spring3&lpar;12&period;4&rpar; - 零配置 之 12&period;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> &lt ...

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

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

  9. &lbrack;译&rsqb;17-spring基于java代码的配置元数据

    spring还支持基于java代码的配置元数据.不过这种方式不太常用,但是还有一些人使用.所以还是很有必要介绍一下. spring基于java代码的配置元数据,可以通过@Configuration注解 ...

随机推荐

  1. &lpar;八&rpar;Eclipse创建Maven项目运行mvn命令

    1.Eclipse创建Maven项目 使用Eclipse创建一个Maven项目非常的简单,选择菜单项File>New>Other(也可以在项目结构空白处右击鼠标键),在弹出的对话框中选择M ...

  2. TcpClient 有好多坑

    下面2篇文章里头的问题都碰到了,真是好坑哈, 在此留念. 使用 TcpClient 與 NetworkStream 類別開發時的注意事項 [C#] NetworkStream.Write()存在严重b ...

  3. Mac OS X常用操作入门指南

    前两天入手一个Macbook air,在装软件过程中摸索了一些基本操作,现就常用操作进行总结, 1关于触控板: 按下(不区分左右)            =鼠标左键 control+按下        ...

  4. animation 动画重播

    <style> div, body{ margin: 0; padding: 0; } body{ background-color: #333; } @keyframes running ...

  5. RabbitMQ(二) -- Work Queues

    RabbitMQ(一) -- Work Queues RabbitMQ使用Work Queues的主要目的是为了避免资源使用密集的任务,它不同于定时任务处理的方式,而是把任务封装为消息添加到队列中.而 ...

  6. bzoj 1324 Exca王者之剑(黑白染色,最小割)

    [题意] 两相邻点不能同时选,选一个点集使得权值和最大. 出题人语文好... [思路] 将图进行黑白二染色,然后构建最小割模型. [代码] #include<set> #include&l ...

  7. spin&period;js无图片实现loading进度条&comma;支持但非依赖jquery

    特点: 1.无图片,无外部CSS 2.无依赖(支持jQuery,但非必须) 3.高度可配置 4.分辨率无关 5.旧版本IE不支持时,采用VML支持 6.使用关键帧动画,采用setTimeout() 7 ...

  8. 在Github上搭建你的博客

    title: blog on github date: 2014-03-24 20:29:47 tags: [blog,github,hexo] --- **用Github写博文** 参考http:/ ...

  9. 【ANT】description元素和属性

    <?xml version="1.0" ?> <project default="test"> <description> ...

  10. &lbrack;oracle&rsqb;查询一个表中数据的插入时间

    select to_char(scn_to_timestamp(ORA_ROWSCN),'yyyy-mm-dd hh24:mi:ss') insert_time from tablename;