|
@Configuration注解主要用于定义配置类.可替换xml配置文件.被注解的类内部包含一个或多个被@Bean注解的方法.
这些方法将会被AnnotationConfigApplicationContext或AnnotationConfigWebApplicationContext类进行扫描.并用于构建bean定义.初始化Spring
容器.
注意:带有@Configuration的注解是配置类.在配置类中写Bean.通过@Bean注解创建Bean对象.有这样一个例子:
配置类:相当于一个Spring容器package com.jin.abc;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.jin.domain.Dog;
@Configuration //注意写法
public class ApplicationConfig {
public ApplicationConfig() {
System.out.println("Spring容器启动");
}
@Bean(name="dog") //定义一个bean,相当于<bean id="dog" class="com.jin.domain">
public Dog getDog(){
return new Dog("小白","白色");
}
} |
这个配置类就相当于下面这个配置文件的写法:
下面是要从该Spring容器中拿到Dog 实例对象
一个具体的应用类:要用到Dog对象.
可以这样拿:
package com.jin.abc;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import com.jin.domain.Dog;
public class TestApplicationConfig {
public static void main(String[] args) {
//以注解的方式获取Spring容器中的对象,只不过该Spring容器的生成是在配置类中,而以前的获取Spring容器的方式是:
/*以前获取Spring容器的方式:
ApplicationContext ac = new ClassPathXmlApplicationContext("spring配置文件.xml");
Dog dog = ac.getBean("dog");
其实,对比这两种方式,各有各的好,xml方式,传统,比较熟悉.注解的方式,比较新颖,灵活,更加方便.轻盈.更加清晰.
*/
AnnotationConfigApplicationContext acac = new AnnotationConfigApplicationContext(ApplicationConfig.class);
Dog dog = (Dog)acac.getBean("dog");
System.out.println(dog);
}
}
|
运行结果:
Spring容器启动
Dog [name=小白, age=白色]
Dog类:
package com.jin.domain;
import java.io.Serializable;
public class Dog implements Serializable {
private String name;
private String age;
public Dog(String name, String age) {
super();
this.name = name;
this.age = age;
}
public Dog() {
super();
// TODO Auto-generated constructor stub
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
@Override
public String toString() {
return "Dog [name=" + name + ", age=" + age + "]";
}
} |
依赖包:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.12</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-messaging</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.2.4.RELEASE</version>
</dependency> |
程序猿的技术大观园:www.javathinker.net
程序猿的技术大观园:www.javathinker.net
|
|