Spring IoC — 基于Java类的配置
来源:互联网 发布:mac上翻译软件 编辑:程序博客网 时间:2024/06/11 05:42
Spring IoC — 基于Java类的配置
普通的POJO只要标注@Configuration注解,就可以为Spring容器提供Bean定义的信息了,每个标注了@Bean的类方法都相当于提供一个Bean的定义信息。
基于Java类的配置方法和基于XML或基于注解的配置方式相比,前者通过代码的方式更加灵活地实现Bean的实例化及Bean之间的装配,但后面两者都是通过配置声明的方式,在灵活性上要稍逊一些,但是配置上要更简单一些。
转自:https://www.cnblogs.com/yangyquin/p/5322591.htmlUserDao类:
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的配置类@Configurationpublic 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;@Configurationpublic 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(); }}
阅读全文
0 0
- Spring IoC — 基于Java类的配置
- Spring 基于注解的IOC配置
- IOC之基于Java类的配置Bean
- IOC之基于Java类的配置Bean
- 基于XML -- Spring IOC配置
- 【Java.Spring.Core】【IoC】基于注解的Bean装配(非XML配置)
- Spring框架总结——第二部分(基于xml配置+注解的方式配置IOC)
- 【Spring】IOC之基于注解的配置bean(上)
- Spring 基于XML配置的IOC入门案例
- Spring 基于注解的IOC配置&整合Junit
- 学习Spring框架笔记之基于xml的IOC配置
- spring/IoC的配置
- Spring的ioc配置
- Spring-基于Java类的配置
- spring的IOC基于annotation
- spring的IOC基于annotation
- spring基于java的配置
- spring 基于java的配置
- FloatingActionButton被软键盘遮挡问题
- iOS-NSJSONReadingMutableContainers,NSJSONReadingMutableLeaves,NSJSONReadingAllowFragments,NSJSONWrit
- 记录一次某医院影像无法正常传输的处理过程
- python里使用正则表达式来分割字符串
- /bin/bash^M: bad interpreter: 没有那个文件或目录
- Spring IoC — 基于Java类的配置
- PHP协程(1):简略
- dismiss到跟视图
- (转)Android启动界面先白屏或者黑屏然后才出现画面的解决办法
- 使用node.js与socket.io搭建即时聊天系统
- Eclipse Java注释模板设置详解
- Celery-4.1 用户指南: Signals
- 面试 jdk代理与cglib代理的区别
- SQL SERVER GETDATE()函数用法