Spring 学习笔记 -beans 的自动扫描与装配和管理
来源:互联网 发布:网上美工兼职 编辑:程序博客网 时间:2024/06/08 18:40
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" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="org.example"/></beans>
调用xml文件代码
ApplicationContext ctx=new ClassPathXmlApplicationContext("beans.xml");PersonService personService=(PersonService)ctx.getBean("personService");personService.save();
personservicebean方法
package cn.cast.service.impl;import javax.annotation.Resource;import org.springframework.stereotype.Service;import cn.cast.service.PersonService;import cn.cast.dao.PersonDao;@Servicepublic class PersonServiceBean implements PersonService {@Resourceprivate PersonDao personDaoBean;public void show() {personDaoBean.show();}public void setPersonDaoBean(PersonDao personDaoBean) {this.personDaoBean = personDaoBean;}public PersonDao getPersonDaoBean() {return personDaoBean;}}
persondaobean方法
package cn.cast.dao.impl;import org.springframework.stereotype.Repository;import cn.cast.dao.PersonDao;@Repository public class PersonDaoBean implements PersonDao { public void show() { System.out.println("执行PersonDaoBean中的add()方法"); } }
测试方法
package cn.cast.main;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import cn.cast.service.PersonService;public class Testmain {public static void main(String args[]){ApplicationContext ctx=new ClassPathXmlApplicationContext("beans.xml");PersonService personService=(PersonService)ctx.getBean("personServiceBean");personService.show();}}
--定义Bean的注解
@Controller
@Controller("Bean的名称")
定义控制层Bean,如Action
@Service
@Service("Bean的名称")
定义业务层Bean
@Repository
@Repository("Bean的名称")
定义DAO层Bean
@Component
定义Bean, 不好归类时使用.
--定义Bean的作用域和生命过程
@Scope("prototype")
值有:singleton,prototype,session,request,session,globalSession
@PostConstruct
相当于init-method,使用在方法上,当Bean初始化时执行。
@PreDestroy
相当于destory-method,使用在方法上,当Bean销毁时执行。
0 0
- Spring 学习笔记 -beans 的自动扫描与装配和管理
- spring的组件扫描和自动装配
- Spring自动扫描与自动装配
- Spring自动扫描和装配bean
- Spring配置组件扫描和自动装配
- Spring学习笔记-自动装配
- spring学习笔记(自动装配)
- Spring装配Bean之组件扫描和自动装配
- Spring装配Bean之组件扫描和自动装配
- Spring装配Bean之组件扫描和自动装配
- Spring学习笔记——Spring的自动装配
- Spring学习笔记(9)----让Spring自动扫描和管理Bean
- Spring学习笔记(9)----让Spring自动扫描和管理Bean
- Spring学习笔记-自动装配与依赖检查
- spring的自动装配和精确装配
- spring 使用注解自动装配和自动扫描机制
- spring自动扫描和管理
- Spring自动扫描装配bean
- Java关键字final、static使用总结
- win7下安装Ubuntu
- watch
- Ext.js正则小结
- 也是枉然
- Spring 学习笔记 -beans 的自动扫描与装配和管理
- 第35课第一题
- 第四届 Contest Print Server
- java面试笔试题
- 【不求甚解之UI篇】毛玻璃效果
- Oracle study之--HASH Cluster特点
- MYSQL修改用户密码
- 十年
- C++中vector的使用