目前Spring主要提供了以下三种装配方式:
一、XML方式配置
1、配置
Spring最早是通过XML方式配置Bean对象,主要实现方式是在项目下创建一个XML文件如 beans.xml,在XML文件声明Bean时,Spring配置文件的根元素来源于Spring beans命名空间所定义<beans>元素。以下为一个经典的Spring 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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd">
<bean id="user" class="com.spring.po.Users" >
<property name="name" value="张三"/>
<property name="age" value="30" />
</bean>
</beans>
2、使用
package com.spring.stud;
import com.spring.po.Users;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Hello world!
*/
public class App {
public static void main(String[] args) {
try {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
Users users = context.getBean("user", Users.class);
System.out.println(users.getName());
} catch (BeansException e) {
e.printStackTrace();
}
}
}
二、注解方式配置
在Spring3.0以后Spring提供了基于Java注解的配置方式。注解的方式是为了最小化XML文件的配置。主要实现方式是。
1、在要装配的类上添加 @Component 注解,如下所示:
package com.spring.po;
import org.springframework.stereotype.Component;
/**
* @Auther admin
* @Date 2018/12/2
* @Description
*/
@Component
public class Address {
//Do something
}
注解说明:
- @Component ——通用的构造型注解,标识该类为Spring组件
- @Controller—— 标识将该类定义为Spring MVC controller
- @Repository—— 标识将该类定义为数据仓库
- @Service——标识将该类定义为服务
- 使用@Component标注的任意自定义注解
2、在beans.xml文件中添加<context:component-scan />,如下所示:
<?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-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<context:component-scan base-package="com.spring.po"/>
</beans>
3、使用同方式
三、基于Java的配置
1、配置
在使用Spring的Java配置可以让我们使用XML就可以编写大多数的Spring配置。我们可以定义一个装配类,如下所示:
package com.spring.stud.config;
import com.spring.po.Users;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @Auther admin
* @Date 2018/12/2
* @Description
*/
@Configuration
public class ConfigBean {
@Bean
public Users users(){
return new Users();
}
}
2、使用
方式一:添加配置文件如同注解配置方式
方式二:如下所示:
package com.spring.stud.config;
import com.spring.po.Users;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/**
* @Auther admin
* @Date 2018/12/2
* @Description
*/
public class ConfigBeanMain {
public static void main(String[] args){
//通过加咱注解的方式装配类文件
ApplicationContext context = new AnnotationConfigApplicationContext(ConfigBean.class);
Users users = context.getBean("users", Users.class);
}
}