Spring IoC — 基于Java类的配置

来源:互联网 发布:mac上翻译软件 编辑:程序博客网 时间:2024/06/11 05:42

Spring IoC — 基于Java类的配置

普通的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的配置类@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();       }}

转自:https://www.cnblogs.com/yangyquin/p/5322591.html
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 电脑f1到f12失灵怎么办 电脑不读读卡器怎么办 a2驾驶本扣分了怎么办 空中网账号忘了怎么办 我注册过借贷宝怎么办 拍拍贷已注册过怎么办 拍拍贷评估额度0怎么办 用钱宝逾期未还怎么办 支付宝被冻结怎么办啊 借钱没借条不还怎么办 借贷宝换号码了怎么办 借贷宝还不起了怎么办 花钱如流水的人怎么办 手机贷登录不上怎么办 如果被网上追逃怎么办 5s指纹排线断了怎么办 苹果6s指纹坏了怎么办 苹果的指纹坏了怎么办 苹果7指纹坏了怎么办 苹果6指纹坏了怎么办 指纹浅打不了卡怎么办 我要贷款5万怎么办 拍拍贷一千不还怎么办 牙龈肿里面有脓怎么办 爱奇艺会员账号忘了怎么办 被私立医院坑了怎么办 在医院被坑了怎么办 流产后子宫内膜薄怎么办 人流后内膜过厚怎么办 子宫内膜薄月经量少怎么办 子宫内膜很薄该怎么办 月经量少子宫内膜薄怎么办 子宫内薄没月经怎么办 感冒20多天不好怎么办 皮肤干燥又痒怎么办了 眼周皮肤很干怎么办 产后掉头发很厉害怎么办 班上学生很吵怎么办 进了网贷黑名单怎么办 预约了挂号没去怎么办 吃完米索手心痒怎么办