SpringBoot学习(三)-----配置Bean

一.配置Bean的方式一

我们可以使用注解@ImportResource引入SpringBean.xml

首先建立一个SpringBoot,添加SpringBootApp.java、beans.xml、HelloService.java、SpringBoottest.java如下:

HelloService.java

1
2
3
4
5
package com.zk.service;
 
public class HelloService {
 
}

beans.xml

1
2
3
4
5
6
7
<?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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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

1
2
//@ImportResource:导入spring配置文件让配置文件的内容生效
@ImportResource(locations={"classpath:beans.xml"})

二.配置Bean的方式二

SpringBootApp.java

1
2
3
4
5
6
7
8
9
10
11
12
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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

1
2
3
4
5
6
7
package com.zk.service;
 
public class HelloService {
    public static void sayHello(){
        System.out.println("Hello,Service");
    }
}

SpringBoottest.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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文件配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<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>

  

 

posted @   leagueandlegends  阅读(513)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
点击右上角即可分享
微信分享提示