一.配置Bean的方式一
我们可以使用注解@ImportResource引入SpringBean.xml
首先建立一个SpringBoot,添加SpringBootApp.java、beans.xml、HelloService.java、SpringBoottest.java如下:
HelloService.java
package com.zk.service; public class HelloService { }
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"> <bean id="HelloService" class="com.zk.service.HelloService"></bean> </beans>
SpringBootApp.java
package com.zk.myspringboot005Controller; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.ImportResource; @SpringBootApplication @ImportResource(locations={"classpath:beans.xml"}) //@ImportResource:导入spring配置文件让配置文件的内容生效 public class SpringBootApp{ public static void main(String[]args){ SpringApplication.run(SpringBootApp.class, args); } }
SpringBoottest.java
package com.zk.myspringboottest; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.Import; import org.springframework.test.context.junit4.SpringRunner; import com.zk.myspringboot005Controller.SpringBootApp; import com.zk.service.HelloService; @RunWith(SpringRunner.class) @SpringBootTest(classes={SpringBootApp.class}) @Import(HelloService.class) public class SpringBoottest { @Autowired private ConfigurableApplicationContext ioc; HelloService helloservice; @Test public void testHelloService(){ System.out.println(ioc.containsBean("HelloService")); } }
运行SpringBoottest.class,运行结果如下:
这里需要注意,使用@ImportResource引入bean.xml
//@ImportResource:导入spring配置文件让配置文件的内容生效 @ImportResource(locations={"classpath:beans.xml"})
二.配置Bean的方式二
SpringBootApp.java
package com.zk.myspringboot005Controller; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.ImportResource; @SpringBootApplication public class SpringBootApp{ public static void main(String[]args){ SpringApplication.run(SpringBootApp.class, args); } }
ConfigBean.java
package com.zk.myspringboot005Controller; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import com.zk.service.HelloService; /* * 指明当前类是配置类,用当前类代替Spring * 在配置文件中使用Bean标签增加组件。 * */ @Configuration public class ConfigBean { @Bean public HelloService HelloService(){ return new HelloService(); } }
HelloService.java
package com.zk.service; public class HelloService { public static void sayHello(){ System.out.println("Hello,Service"); } }
SpringBoottest.java
package com.zk.myspringboottest; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.context.ApplicationContext; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.test.context.junit4.SpringRunner; import com.zk.myspringboot005Controller.SpringBootApp; import com.zk.service.HelloService; @RunWith(SpringRunner.class) @SpringBootTest(classes={SpringBootApp.class}) public class SpringBoottest { @Autowired private ConfigurableApplicationContext ioc; HelloService helloservice; @Autowired private ApplicationContext ac; @Test public void testHelloService(){ System.out.println(ioc.containsBean("HelloService")); helloservice.sayHello(); boolean flag=ac.containsBean("HelloService"); System.out.println(flag); } }
运行结果如下:
pom.xml文件配置
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.zk.myspringboot_003</groupId> <artifactId>myspringBoot_003</artifactId> <version>0.0.1-SNAPSHOT</version> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <build> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> <finalName>myspringboot_003</finalName> </build> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency> <!-- 继承父包 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-autoconfigure</artifactId> </dependency> </dependencies> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.3.RELEASE</version> </parent> </project>