从头认识Spring-2.8 基于java注解的配置(基本没有xml)

时间:2023-02-01 19:03:50

我们这一章节来讨论一下基于java注解的配置(基本没有xml)。

1.domain

厨师类:

package com.raylee.my_new_spring.my_new_spring.ch02.topic_1_22;

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

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;
}

}

没有太大的变化,只是增加一个id,以示区别。


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

package com.raylee.my_new_spring.my_new_spring.ch02.topic_1_22;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;@Configurationpublic class SpringBeans {@Beanpublic Chief jack() {Chief chief = new Chief();chief.setName("mike");return chief;}}

@Configuration代表配置文件

@Bean跟我们之前用的<bean/>是一致的


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


2.测试类:

package com.raylee.my_new_spring.my_new_spring.ch02.topic_1_22;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;@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(locations = {"/com/raylee/my_new_spring/my_new_spring/ch02/topic_1_22/ApplicationContext-test.xml" })public class ChiefTest {@Autowiredprivate ApplicationContext applicationContext;@Testpublic void testChief() {Chief jack1 = (Chief) applicationContext.getBean(Chief.class);System.out.println(jack1.getId());Chief jack2 = (Chief) applicationContext.getBean("jack");System.out.println(jack2.getId());}}

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


3.配置文件

<?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.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsdhttp://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsdhttp://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-scanbase-package="com.raylee.my_new_spring.my_new_spring.ch02.topic_1_22" /></beans>

只有一个自动扫描,算是最少的配置了


测试输出:

0

0


总结:这一章节简单说明一下基于java 的配置,由于所有标签都是跟xml的功能一致,因此在这里不再详细说明标签里面的属性。


目录:http://blog.csdn.net/raylee2007/article/details/50611627 

 

我的github:https://github.com/raylee2015/my_new_spring