Spring的Java配置方式—@Configuration和@Bean实现Java配置

Spring的Java配置方式—@Configuration和@Bean实现Java配置

Java配置是Spring4.x推荐的配置方式,可以完全替代xml配置。

1、@Configuration 和 @BeanSpring的Java配置方式是通过 @Configuration 和 @Bean 注解实现的:a、@Configuration 作用于类上,相当于一个xml配置文件b、@Bean 作用于方法上,相当于xml配置中的

2、Java配置示例演示通过Java配置的方式进行Spring配置,并且实现Spring IOC功能。2.1) 创建工程以及导入依赖(Maven)

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

com.lynch

spring-config

0.0.1-SNAPSHOT

war

org.springframework

spring-webmvc

4.3.7.RELEASE

com.jolbox

bonecp-spring

0.8.0.RELEASE

${project.artifactId}

org.apache.maven.plugins

maven-resources-plugin

UTF-8

org.apache.maven.plugins

maven-compiler-plugin

1.8

1.8

UTF-8

org.apache.maven.plugins

maven-war-plugin

2.6

false

org.apache.tomcat.maven

tomcat7-maven-plugin

2.2

2.2) 编写User Model对象

package com.lynch.javaconfig;

public class User {

private String username;

private String password;

private Integer age;

public String getUsername() {

return username;

}

public void setUsername(String username) {

this.username = username;

}

public String getPassword() {

return password;

}

public void setPassword(String password) {

this.password = password;

}

public Integer getAge() {

return age;

}

public void setAge(Integer age) {

this.age = age;

}

}

2.3) 编写UserDAO,用于模拟与数据库的交互(注意此DAO没有打注解)

package com.lynch.javaconfig;

import java.util.ArrayList;

import java.util.List;

public class UserDAO {

public List queryUserList() {

// 模拟数据库的查询

List result = new ArrayList();

for (int i = 0; i < 10; i++) {

User user = new User();

user.setUsername("username_" + i);

user.setPassword("password_" + i);

user.setAge(i + 1);

result.add(user);

}

return result;

}

}

2.4) 编写UserService,用于实现User数据操作业务逻辑(声明service注解,并自动注入dao对象)

package com.lynch.javaconfig;

import java.util.List;

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

import org.springframework.stereotype.Service;

@Service

public class UserService {

// 注入Spring容器中的bean对象

@Autowired

private UserDAO userDAO;

public List queryUserList() {

// 调用userDAO中的方法进行查询

return this.userDAO.queryUserList();

}

}

2.5) 编写SpringConfig,用于实例化Spring容器打上@Configuration注解,同时打上@ComponentScan配置扫描的包。

@Bean用于向容器中注入对象,如果在UserDao类前面打上@Repository注解就不用@Bean方式

package com.lynch.javaconfig;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.ComponentScan;

import org.springframework.context.annotation.Configuration;

//通过@Configuration注解来表明该类是一个Spring的配置,相当于一个xml文件

@Configuration

@ComponentScan(basePackages = "com.lynch.javaconfig")

public class SpringConfig {

// 通过@Bean注解来表明是一个Bean对象,相当于xml中的

@Bean

public UserDAO getUserDAO() {

return new UserDAO(); // 直接new对象做演示

}

}

注意:方法名是作为返回对象的名字的,因此一般不带get,也就是上述放入spring容器的bean的name为getUserDAO

2.6) 编写测试方法,用于启动Spring容器

package com.lynch.javaconfig;

import java.util.List;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Application {

public static void main(String[] args) {

// 通过Java配置来实例化Spring容器

AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);

// 在Spring容器中获取Bean对象

UserService userService = context.getBean(UserService.class);

// 调用对象中的方法

List list = userService.queryUserList();

for (User user : list) {

System.out.println(user.getUsername() + ", " + user.getPassword() + ", " + user.getPassword());

}

// 销毁该容器

context.destroy();

}

}

执行结果:

username_0, password_0, password_0

username_1, password_1, password_1

username_2, password_2, password_2

username_3, password_3, password_3

username_4, password_4, password_4

username_5, password_5, password_5

username_6, password_6, password_6

username_7, password_7, password_7

username_8, password_8, password_8

username_9, password_9, password_9

总结:从以上的示例中可以看出,使用Java代码就完美的替代xml配置文件,并且结构更加的清晰。

3、使用方法Spring对Java配置的支持是由@Configuration注解和@Bean注解来实现的。@Bean注解将会实例化、配置和初始化一个新对象,这个对象将由Spring的IoC容器来管理。@Bean声明所起到的作用与 元素类似。@Configuration注解的类表示这个类的主要目的是作为bean定义的资源。被@Configuration声明的类可以通过在同一个类的内部调用@bean方法来设置嵌入bean的依赖关系。

@Configuration

public class AppConfig{

@Bean

public MyService myService() {

return new MyServiceImpl();

}

}

对于上面的@Beans配置文件,等同于下面的XML配置文件:

上述配置方式的实例化方式如下:利用AnnotationConfigApplicationContext 类进行实例化

public static void main(String[] args) {

ApplicationContext ctx = new AnnotationConfigApplicationContext(AppConfig.class);

MyService myService = ctx.getBean(MyService.class);

myService.doStuff();

}

要使用组件扫描,仅需用@Configuration进行注解即可:

@Configuration

@ComponentScan(basePackages = "com.lynch")

public class AppConfig {

...

}

在上面的例子中,com.lynch包首先会被扫描到,然后在容器内查找被@Component 声明的类,找到后将这些类按照Sring bean定义进行注册。

相关推荐

历届世界杯时间表(历届世界杯赛程表)
bte365体育

历届世界杯时间表(历届世界杯赛程表)

🗓️ 06-28 👁️ 9812
两公升水是多少?
beat365平台正版

两公升水是多少?

🗓️ 09-21 👁️ 838
吃鸡的钢枪是什么意思
365bet盘口

吃鸡的钢枪是什么意思

🗓️ 09-29 👁️ 8837

友情链接