spring boot基于Java的容器配置讲解

论坛 期权论坛     
niminba   2021-5-23 02:01   249   0
<p>spring容器是负责实例化、配置、组装组件的容器。</p>
<p>容器的配置有很多,常用的是xml、Java注解和Java代码。</p>
<p>在spring中Ioc容器相关部分是context和beans中。其中context-support保存着许多线程的容器实现。比如AnnotationConfigApplicationContext或者ClassPathXmlApplicationContext。两者只有接收的目标不同,前者接收Java类后者接收Xml文件。但作为spring容器的不同实现殊途同归。</p>
<p>下面我通过<a href="https://lfvepclr.gitbooks.io/spring-framework-5-doc-cn/content/" rel="external nofollow" target="_blank">spring文档</a>中的介绍来自己过一遍容器配置,来加深印象。这里我使用的是springboot。所以有些基于web.xml的配置不会涉及到。</p>
<p><span style="color: #ff0000"><strong>@Bean和@Configuration</strong></span><br>
</p>
<p>@Configuration注解的类,表示这个类是一个配置类,类似于&lt;beans&gt;&lt;/beans&gt;或者.xml文件。</p>
<p>@Bean注解用来说明使用springIoc容器管理一个新对象的实例化、配置和初始化。类似于&lt;bean&gt;&lt;/bean&gt;,默认情况下,bean名称就是方法名称.</p>
<p>例子:</p>
<div class="blockcode">
<pre class="brush:java;">
@Configuration
public class Conf {
  
  @Bean
  public HelloService helloService() {
    return new HelloServiceImpl();
  }
  
}

</pre>
</div>
<p>这种配置方式就类似于xml配置中的</p>
<div class="blockcode">
<pre class="brush:xml;">
&lt;beans&gt;
  &lt;bean id="helloService" class="com.dust.service.impl.HelloServiceImpl" /&gt;
&lt;/beans&gt;

</pre>
</div>
<p>等价于注解配置中的</p>
<div class="blockcode">
<pre class="brush:java;">
@Service
public class HelloServiceIMpl implements HelloService {
  
  @Override
  public String hello() {
    return "hello world";
  }
  
}

</pre>
</div>
<p><span style="color: #ff0000"><strong>使用AnnotationConfigApplicationContext实例化Spring容器</strong></span><br>
</p>
<p>这是在spring3.0加入的功能,除了接收@Configuration注解的类作为输入类之外还可以接受使用JSR-330元数据注解的简单类和@Component类。</p>
<p>当@Configuration注解的类作为输入时,@Configuration类本身会被注册为一个bean,在这个类中所有用@Bean注解的方法都会被定义为一个bean。</p>
<p>具体有哪些类型的bean可以方法遍历打印容器中的bean。</p>
<div class="blockcode">
<pre class="brush:java;">
  public static void main(String[] args) {
    ApplicationContext context = new AnnotationConfigApplicationContext(Conf.class);
    HelloService helloService = context.getBean(HelloService.class);
    String hello = helloService.hello();
    System.out.println(hello);
  }

</pre>
</div>
<p>该实例的步骤为:</p>
<p>1. 创建AnnotationConfigApplicationContext容器对象,同时将@Configuration注解的Conf.class作为参数传入。<br>
2. 容器回根据传入的Conf类来构建bean。其中就有helloService<br>
3. 通过bean的对象类型获取到容器中保管的对象。<br>
4. 执行对象方法</p>
<p>但是AnnotationConfigApplicationContext并不仅使用@Configuration类。任何@Component或JSR-330注解的类都可以作为输入提供给构造函数。例如:</p>
<div class="blockcode">
<pre class="brush:java;">
  public static void main(String[] args) {
    ApplicationContext context = new AnnotationConfigApplicationContext(HelloServiceImpl.class, A.class, B.class);
    HelloService helloService = context.getBean(HelloService.class);
    String hello = helloService.hello();
    System.out.println(hello);
  }

</pre>
</div>
<p>上面假设MyServiceImpl、A和B都用了Spring的依赖注入的注解,例如@Autowired。</p>
<p><strong>使用register(Class&lt;&#63;&gt;…)的方式构建容器</strong><br>
</p>
<p>也可以使用无参构造函数实例化AnnotationConfigApplicationContext,然后使用register()方法配置。当使用编程方式构建AnnotationConfigApplicationContext时,这种方法特别有用。</p>
<p>例子:</p>
<div class="blockcode">
<pre class="brush:java;">
  public static void main(String[] args) {
    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
    context.register(Conf.class);
    context.refresh();
    HelloService helloService = context.getBean(HelloService.class);
    String hello = helloService.hello();
    System.out.println(hello);
  }</pre>
</div>
<p>其中的refresh方法是一个初始化工作。否则注册的类并不会被生成bean。</p>
<p><strong>使用scan(String …)组件扫描</strong><br>
</p>
<p>组件扫描,只需要设置好对应包路径,spring容器回自动扫描包下面所有能够被容器初始化的Java类。</p>
<p>使用注解:</p>
<div class="blockcode">
<pre class="brush:java;">
@Configuration
@ComponentScan("com.example.springdemo.beans")
public class Conf {

  @Bean
  public HelloService helloService() {
    //用这种方法创建的service相当于用@Service注解标注
    return new HelloServiceImpl();
  }

}

</pre>
</div>
<p>在该路径下还有一个配置文件:</p>
<div class="blockcode">
<pre class="brush:java;">
@Configuration
public class Conf2 {

  @Bean
  public ByeService byeService() {
    //用这种方法
分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:1060120
帖子:212021
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP