Spring:IoC 用法(五、混合配置用法)
来源:互联网 发布:mac dock栏 图标合并 编辑:程序博客网 时间:2024/06/06 18:20
混合配置
Java配置与XML配置交叉使用
1、多个Java配置类
1)、定义Bean
模拟工作中的业务请求:
定义Controller控制层
@Controllerpublic class TestController { @Autowired private TestRepository repository; // 测试注入方法 public String testController() { return repository.testRepository(); }}
定义Repository数据层接口
public interface TestRepository { // 测试注入:调用方法 String testRepository();}
定义Repository数据层接口实现
@Repositorypublic class TestRepositoryImpl implements TestRepository { // 测试注入方法 public String testRepository() { return "------------------------- >>> 获取数据成功!!"; }}
2)、Java配置注入
专门扫描 Controller 配置
@Configuration@ComponentScan(basePackages = "test.define.controller")public class ControllerConfig {}专门扫描 Repository 配置
@Configuration@ComponentScan(basePackages = "test.define.repository")public class RepositoryConfig {}组合以上两个配置
@Configuration@Import({ControllerConfig.class,RepositoryConfig.class})public class Config {}
3)、测试代码
依赖
<properties><spring.version>4.2.1.RELEASE</spring.version></properties><dependencies><!-- Spring 依赖注入 --><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>${spring.version}</version></dependency><!-- 仅测试 --><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version></dependency></dependencies>
测试类
public class Test { @org.junit.Test public void test() { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Config.class); TestController controller = context.getBean(TestController.class); String data = controller.testController(); System.out.println("结果:" + data); context.close(); }}结果
结果:------------------------- >>> 获取数据成功!!
2、多个XML配置文件
1)、定义Bean
(同上不变)
2)、XML配置注入
专门扫描Controller配置
<?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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="test.define.controller"/></beans>
专门扫描Repository配置
<?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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="test.define.repository"/></beans>
组合以上两个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.xsd"> <import resource="ControllerConfig.xml"/> <import resource="RepositoryConfig.xml"/></beans>
3)、测试代码
依赖(不变)
测试类
public class Test { @org.junit.Test public void test1() { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring/spring.xml"); TestController controller = context.getBean(TestController.class); String data = controller.testController(); System.out.println("结果:" + data); context.close(); }}
结果(不变)
3、Java配置与XML配置(Java为主)
1)、定义Bean
(同上不变)
2)、配置
专门扫描Controller配置
@Configuration@ComponentScan(basePackages = "test.define.controller")public class ControllerConfig {}
专门扫描Repository配置
<?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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="test.define.repository"/></beans>
Java配置组合以上两个配置
@Configuration@Import(ControllerConfig.class)@ImportResource("classpath:spring/RepositoryConfig.xml")public class Config {}
3)、测试代码
依赖(不变)
测试类
public class Test { @org.junit.Test public void test() { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Config.class); TestController controller = context.getBean(TestController.class); String data = controller.testController(); System.out.println("结果:" + data); context.close(); }}结果(不变)
4、XML配置与Java配置用(XML为主)
1)、定义Bean
(同上不变)
2)、配置
专门扫描Controller配置
<?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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="test.define.controller"/></beans>
专门扫描Repository配置
@Configuration@ComponentScan(basePackages = "test.define.repository")public class RepositoryConfig {}
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.xsd"> <import resource="ControllerConfig.xml"/> <bean class="test.config.RepositoryConfig"/></beans>
3)、测试代码
依赖(不变)
测试类
public class Test { @org.junit.Test public void test() { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring/spring.xml"); TestController controller = context.getBean(TestController.class); String data = controller.testController(); System.out.println("结果:" + data); context.close(); }}结果(不变)
阅读全文
0 0
- Spring:IoC 用法(五、混合配置用法)
- Spring:IoC 用法(三、XML配置用法)
- Spring:IoC 用法(四、Java配置用法)
- Spring:IoC 用法(七、<beans>用法)
- Spring:IoC 用法(九、@Bean 用法)
- Spring:IoC 用法(八、<bean>用法)
- Spring的IOC用法
- Java Spring IOC用法
- Spring IOC详细用法
- Java Spring IOC用法
- Spring:IoC 用法(一、大纲)
- Spring:IoC 用法(二、自动注入用法)
- Spring:IoC 用法(六、自动扫描Bean用法)
- Spring之IOC基本用法
- Spring Boot参考教程(五)Spring Boot配置使用之配置类用法
- Spring核心技术--IoC container用法详解
- spring+mybatis的配置用法
- Spring复习(二)IOC注解配置
- mongodb 简介以及常用方法小结
- 苹果应用技术支持
- RabbitMQ (八) 远程调用(RPC)
- 矩阵快速幂
- Java对不同版本的word,excel的解析
- Spring:IoC 用法(五、混合配置用法)
- String常量池
- 修改AlertDialog的宽高大小,Button的字体颜色等
- Linux查看MySQL的安装路径
- 如何在tomcat下部署php
- Codeforces Round #423 B. Black Square
- CSS多行文字垂直居中的方法
- spring实现定时器功能
- POJ 2431-Expedition(优先队列)