Spring学习十一----------Bean的配置之基于Java的容器注解@Bean

本文内容纲要:Spring学习十一----------Bean的配置之基于Java的容器注解@Bean

© 版权声明:本文为博主原创文章,转载请注明出处

@Bean

-@Bean标识一个用于配置和初始化一个由SpringIOC容器管理的新对象的方法,类似于XML配置文件的

-可以在Spring的@Component注解的类中使用@Bean注解任何方法(仅仅是可以)

-在上一点中,通过使用的是@Configuration

实例

1.项目结构

Image

2.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/maven-v4_0_0.xsd">

<modelVersion>4.0.0</modelVersion>

<groupId>org.springbean</groupId>

<artifactId>Spring-JavaBean</artifactId>

<packaging>war</packaging>

<version>0.0.1-SNAPSHOT</version>

<name>Spring-JavaBean Maven Webapp</name>

<url>http://maven.apache.org</url>

<properties>

<spring.version>4.3.8.RELEASE</spring.version>

</properties>

<dependencies>

<!-- junit依赖 -->

<dependency>

<groupId>junit</groupId>

<artifactId>junit</artifactId>

<version>4.12</version>

<scope>test</scope>

</dependency>

<!-- spring核心依赖 -->

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-core</artifactId>

<version>${spring.version}</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-beans</artifactId>

<version>${spring.version}</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-context</artifactId>

<version>${spring.version}</version>

</dependency>

</dependencies>

<build>

<finalName>Spring-JavaBean</finalName>

</build>

</project>

3.spring-javabean.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.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context.xsd">

<context:component-scan base-package="org.spring.javabean"/>

</beans>

4.Store.java

package org.spring.javabean;

public interface Store {

}

5.StringStore.java

package org.spring.javabean;

public class StringStore implements Store {

public void init() {

System.out.println("执行了init方法");

}

public void destroy() {

System.out.println("执行了destroy方法");

}

}

6.StoreConfig.java

package org.spring.javabean;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

@Configuration

public class StoreConfig {

@Bean(name="stringStore", initMethod="init", destroyMethod="destroy")

public Store stringStore() {

return new StringStore();

}

}

7.TestBase.java

package org.spring.javabean.test;

import org.junit.After;

import org.junit.Before;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import org.springframework.util.StringUtils;

public class TestBase {

private ClassPathXmlApplicationContext context;

private String springXmlPath;

/**

* 无参构造器

*/

public TestBase() {

}

/**

* 含参构造器,初始化spring配置文件路径

*

* @param springXmlPath

* spring配置文件路径

*/

public TestBase(String springXmlPath) {

this.springXmlPath = springXmlPath;

}

/**

* 初始化加载spring配置文件到IOC容器中

*/

@Before

public void before() {

if(StringUtils.isEmpty(springXmlPath)){

springXmlPath = "classpath:spring-*.xml";

}

context = new ClassPathXmlApplicationContext(springXmlPath.split("[,\\s]+"));

context.start();

}

/**

* 销毁IOC容器

*/

@After

public void after() {

if(context != null){

context.destroy();

}

}

/**

* 通过bean ID获取bean对象

*

* @param beanId

* bean ID

* @return

*/

public Object getBean(String beanId) {

return context.getBean(beanId);

}

}

8.TestSpringJavaBean.java

package org.spring.javabean.test;

import org.junit.Test;

import org.spring.javabean.Store;

public class TestSpringJavaBean extends TestBase {

public TestSpringJavaBean() {

super("classpath:spring-javabean.xml");

}

@Test

public void testJavaBean() {

Store store = (Store) super.getBean("stringStore");

System.out.println(store.getClass().getName());

}

}

9.效果预览

Image

参考:http://www.imooc.com/video/4033

本文内容总结:Spring学习十一----------Bean的配置之基于Java的容器注解@Bean

原文链接:https://www.cnblogs.com/jinjiyese153/p/6756805.html

以上是 Spring学习十一----------Bean的配置之基于Java的容器注解@Bean 的全部内容, 来源链接: utcz.com/z/296486.html

回到顶部