您的位置:首页 > 编程语言 > Java开发

Spring基于Java注解的配置

2017-01-23 20:33 393 查看
前面已经讲了自动扫包配置和基于XML的配置,这一节讲基于Java的配置

(1)entity

厨师类,增加了唯一id

package spring.ch2.topic8;

import org.springframework.beans.factory.annotation.Value;

/**
* Created by louyuting on 17/1/23.
*/
public class Chief {
private final int id = index++;

private static int index = 0;

public int getId() {
return id;
}

@Value("jack")
private String name = "";

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}


SpringBeans类:主要用来创建各种bean:

package spring.ch2.topic8;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
* Created by louyuting on 17/1/23.
*/
@Configuration
public class SpringBeans {
@Bean
public Chief jack() {
Chief chief = new Chief();
chief.setName("mike");
return chief;
}
}


@Configuration 代表这是一个配置文件,

@Bean跟我们之前用的 XML中的bean标签是一致的。

基于java的配置有一个好处,就是在注入属性值的时候更加直观,不再是用property/>标签

(2)配置文件:只有一个自动扫包。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">

<context:component-scan base-package="spring.ch2.topic8" />
</beans>


(3)测试类;

package spring.ch2.topic8;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

/**
* Created by louyuting on 17/1/20.
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"/spring/ch2/topic8/ApplicationContext-test.xml"})
public class ChiefTest {
@Autowired
private ApplicationContext applicationContext;

@Test
public void testChief(){
Chief jack1 = (Chief)applicationContext.getBean(Chief.class);
System.out.println(jack1.getId());

Chief jack = (Chief)applicationContext.getBean("jack");
System.out.println(jack.getId());
}

}


这里我们稍许改动,因为我们只是注册了一个bean,但是我们两次取出bean的id,目的是为了测试使用java配置的时候,产生的bean是否也是单例,因为在配置文件使用了new,这里会产生一些歧义。

(4)运行结果:

0
0


两次输出的id是一样的,说明基于Java的装配bean默认也是单例的。

完整代码的github地址。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  spring