spring基于java配置@Configuration和@Bean用法【代码示例】

来源:互联网 发布:mac哪个国家的缩写 编辑:程序博客网 时间:2024/05/19 18:15



public class MyDriverManager {public MyDriverManager(String url,String userName,String password){System.out.println("url="+url);System.out.println("username="+userName);System.out.println("password="+password);}}


import org.springframework.beans.factory.annotation.Value;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.ImportResource;@Configuration@ImportResource("classpath:config.xml")public class StoreConfig {@Value("${jdbc.url}")private String url;@Value("${jdbc.username}")private String username;@Value("${jdbc.password}")private String password;@Beanpublic MyDriverManager myDriverManager(){return new MyDriverManager(url,username,password); }}






@ImportResource("")是获得xml文件,但在xml文件中又加载了config.properties配置文件,

所以也就间接的加载了config.properties配置文件


@Value("${key-name}")  将配置文件中的值保存在指定字段中

这两个注解加在一起无非就是完成从配置文件中获取键值对的功能,可以用xml文件实现

<context:property-placeholder location="classpath:jdbc.properties"/><bean class="org.Springframework.jdbc.datasource.DriverManagerDataSource"><property name="url" value="${"jdbc.url"}"/></bean>






@Bean标识一个用于配置和初始化一个由springIOC容器管理的新对象,其作用类似于xml文件中的<bean></bean> 

添加了此注解 就无须在MyDriverManager类中添加那四个注解中的一个了

(传说中的四个注解:@Component @Repository @Service @Controller)

若没有(name="  ")则默认id或称之为name为方法的名称



config.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:property-placeholder location="classpath:/config.properties" /></beans>






config.properties配置文件

jdbc.url=127.0.0.1jdbc.username=rootjdbc.password=root





注意文件中的username前加jdbc.是很必要的!



单元测试类:

import org.junit.Test;import org.junit.runner.RunWith;import org.junit.runners.BlockJUnit4ClassRunner;import test.UnitTestBase;@RunWith(BlockJUnit4ClassRunner.class)public class TestBean extends UnitTestBase{public TestBean(){super("classpath:spring-bean-annotations.xml");}@Testpublic void test(){MyDriverManager driver=super.getBean("myDriverManager");System.out.println(driver.getClass().getName());}}







spring-bean-annotations.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="test10"></context:component-scan> </beans>



整体理一下思路


1.加载的spring-bean-annotations.xml文件是为了可以在test10包中找到MyDriverManager类

但如果给类加@Component注解会有异常


2.通过“myDriverManager”获得实例对象,因为在StoreConfig类中

有加了@Bean注解的 得到一个MyDriverManager实例对象的方法

并且其构造器的参数声明为成员变量,是通过加载config.xml文件得到的

3.在congif.xml文件中加载了config.properties资源文件,获得其中的键值对



0 0