Spring-自动扫描管理bean

来源:互联网 发布:java流程图 编辑:程序博客网 时间:2024/05/03 00:22

1、创建bean

/* *@Author swxctx *@time 2016年9月27日 */package com.sw.service;import org.springframework.stereotype.Controller;@Controller(value="personServiceBean")public class personServiceBean {public void save(){System.out.println("PersonServiceBean");}}

/* *@Author swxctx *@time 2016年9月27日 */package com.sw.service1;import javax.annotation.PostConstruct;import javax.annotation.Resource;import org.springframework.stereotype.Controller;import com.sw.service.personServiceBean;@Controller(value="userServiceBean")public class userServiceBean {@Resourcepublic personServiceBean psb=null;public static personServiceBean createPerson(){return new personServiceBean();}@PostConstructpublic void add(){psb.save();}}

3、配置文件

<?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-2.5.xsd           http://www.springframework.org/schema/context           http://www.springframework.org/schema/context/spring-context-2.5.xsd"><context:component-scan base-package="com.sw.service"></context:component-scan><context:component-scan base-package="com.sw.service1"></context:component-scan></beans>


0 0
原创粉丝点击