项目结构 ,其他的具体文件就不展开介绍了。
一、基于XML的配置方式
1、xml文件的bean声明
<?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.xsd">
<bean id="user" class="example.po.pojo1.User" lazy-init="true" scope="singleton"
init-method="myUserInit" destroy-method="myUserDestroy">
<!-- 属性名和类的属性名字一致 -->
<property name="userName" value="张三"></property>
<property name="passWord" value="123456"></property>
</bean>
</beans>
在XML配置中,通过<bean></bean>来定义Bean,通过id或name属性定义Bean的名称,如果未指定id和name属性,Spring则自动将全限定类名作为Bean的名称。通过<property>子元素或者p命名空间的动态属性为Bean注入值。还可以通过<bean>的init-method和destory-method属性指定Bean实现类的方法名来设置生命过程方法(最多指定一个初始化方法和销毁方法)。通过<bean>的scope指定Bean的作用范围。听过<bean>的lazy-init属性指定是否延迟初始化。
当Bean的实现类来源于第三方类库,比如DataSource、HibernateTemplate等,无法在类中标注注解信息,只能通过XML进行配置;而且命名空间的配置,比如aop、context等,也只能采用基于XML的配置。
2、Java bean类
public class User {
private String userName;
private String passWord;
// 用于设置初始化方法
public void myUserInit() {
System.out.println("***初始化开始 User **");
}
// 用于设置销毁方法
public void myUserDestroy() {
System.out.println("**销毁方法 User***");
}
//getter setter方法
}
二、基于注解的配置
dispatcher-servlet.xml文件中 定义扫描装配
<!--自动扫描装配 ,扫描所有定义的bean(注解定义的,
比如 controller,Service,Repository,Component) -->
<context:component-scan base-package="example.po.pojo2,example.controller"></context:component-scan>
package example.po.pojo2;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
@Scope("prototype")
@Lazy(true)
@Component("person")
public class Person {
private String personName;
private String age;
// 用于设置初始化方法
@PostConstruct
public void myPersonInit() {
System.out.println("***初始化开始 Person **");
}
// 用于设置销毁方法
@PreDestroy
public void myPersonDestroy() {
System.out.println("**销毁方法 Person***");
}
//setter getter方法
}
在Bean实现类中通过一些Annotation来标注Bean类:
·@Component:标注一个普通的SpringBean类(可以指定Bean名称,未指定时默认为小写字母开头的类名)
·@Controller:标注一个控制器类
·@Service:标注一个业务逻辑类
·@Repository:标注一个DAO类
通过在成员变量或者方法入参处标注@Autowired按类型匹配注入,也可以使用@Qualifier按名称配置注入。通过在方法上标注@PostConstrut和PreDestroy注解指定的初始化方法和销毁方法(可以定义任意多个)。通过@Scope(“prototype”)指定Bean的作用范围。通过在类定义处标注@Lazy(true)指定Bean的延迟加载。
当Bean的实现类是当前项目开发的,可以直接在Java类中使用基于注解的配置,配置比较简单。
三、基于Java类配置
package example.controller;
import example.po.pojo3.People;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
@Configuration
@ComponentScan(basePackages = "example.po.pojo3")
public class AppConf {
@Scope("prototype")
@Bean
public People people() {
People people = new People();
people.setPeopleName("里斯");
people.setSex("男");
return people;
}
}
package example.po.pojo3;
public class People {
private String peopleName;
private String sex;
//setter getter方法
}
@Configuration:这个注解其实就是把我们当前的这个类,声明为一个配置文件,它就相当于我们的xml配置文件,跟它的作用是一样的,只不过用类的方式来进行展现;
@ComponentScan(basePackages="包路径"):这个注解其实就是去扫描我们的包路径,对我们需要扫描的包进行扫描,如果需要扫描多个包,之间用逗号隔开即可;除此之外,还可以指定某一个具体的类,例如:@ComponentScan(basePackageClasses = People.class);
@Bean:这个注解其实更好理解,它就相当于xml中的<bean>,我们之前通过<bean>标签将它注入到容器中,现在只需要在你所需要注入的资源上面加上@Bean注解,就可以注入到Spring容器中。
在标注了@Configuration的java类中,通过在类方法标注@Bean定义一个Bean。方法必须提供Bean的实例化逻辑。通过@Bean的name属性可以定义Bean的名称,未指定时默认名称为方法名。在方法处通过@Autowired使方法入参绑定Bean,然后在方法中通过代码进行注入;也可以调用配置类的@Bean方法进行注入。通过@Bean的initMethod或destroyMethod指定一个初始化或者销毁方法。通过Bean方法定义处标注@Scope指定Bean的作用范围。通过在Bean方法定义处标注@Lazy指定Bean的延迟初始化。
当实例化Bean的逻辑比较复杂时,则比较适合基于Java类配置的方式。
四、调用
package example.controller;
import example.po.pojo2.Person;
import example.po.pojo1.User;
import example.po.pojo3.People;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class IndexController {
@Autowired
User user ;
@Autowired
Person person ;
@Autowired
People people ;
@RequestMapping("/index")
public String index() {
System.out.println("通过xml配置的bean注入------User");
System.out.println(user.getUserName()+" "+user.getPassWord());
System.out.println("通过注解配置的bean注入------Person");
System.out.println(person.getPersonName()+" "+person.getAge());
System.out.println("通过注解配置的bean注入------people");
System.out.println(people.getPeopleName()+" "+people.getSex());
return "main";
}
}
REF :
Spring学习() Bean配置的三种方式(XML、注解、Java类)介绍与对比
示例代码: