spring装配bean(基于注解)
来源:互联网 发布:mysql修复数据表 编辑:程序博客网 时间:2024/05/17 01:40
装配bean(基于注解)
l 注解:就是一个类,使用@注解名称
l 开发中:使用注解 取代 xml配置文件。
l 注解使用前提,添加命名空间,让spring扫描含有注解类
1. @Component
@Component取代<bean class="">
@Component("id")取代 <beanid="" class="">
UserService:
package com.itheima.g_annotation.a_ioc;public interface UserService {public void addUser();}
UserServiceImpl:
package com.itheima.g_annotation.a_ioc;import org.springframework.stereotype.Component;@Component("userServiceId")public class UserServiceImpl implements UserService {@Overridepublic void addUser() {System.out.println("g_annotation.a_ioc add user");}}
beans.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="com.itheima.g_annotation.a_ioc"></context:component-scan></beans>
TestAnnoIoC:
package com.itheima.g_annotation.a_ioc;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class TestAnnoIoC {@Testpublic void demo02(){//从spring容器获得String xmlPath = "com/itheima/g_annotation/a_ioc/beans.xml";ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);UserService userService = (UserService) applicationContext.getBean("userServiceId");userService.addUser();}}
2.web开发
提供3个@Component注解衍生注解(功能一样)取代<beanclass="">
@Repository:dao层
@Service:service层
@Controller:web层
3.依赖注入
给私有字段设置,也可以给setter方法设置
普通值:@Value("")
引用值:
方式1:按照【类型】注入
@Autowired
方式2:按照【名称】注入1
@Autowired
@Qualifier("名称")
方式3:按照【名称】注入2
@Resource("名称")
StudentDao:
package com.itheima.g_annotation.b_web;public interface StudentDao {void save();}
StudentDaoImpl:
package com.itheima.g_annotation.b_web;import org.springframework.stereotype.Repository;@Repository("studentDaoId")public class StudentDaoImpl implements StudentDao {@Overridepublic void save() {System.out.println("dao");}}
StudentService:
package com.itheima.g_annotation.b_web;public interface StudentService {void addStudent();}
StudentServiceImpl:
package com.itheima.g_annotation.b_web;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Qualifier;import org.springframework.stereotype.Service;@Servicepublic class StudentServiceImpl implements StudentService {private StudentDao studentDao;@Autowired@Qualifier("studentDaoId")public void setStudentDao(StudentDao studentDao) {this.studentDao = studentDao;}@Overridepublic void addStudent() {studentDao.save();}}
StudentAction:
package com.itheima.g_annotation.b_web;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;@Controller("studentActionId")public class StudentAction {@Autowired //默认按照类型private StudentService studentService;public void execute() {studentService.addStudent();}}
beans.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="com.itheima.g_annotation.b_web"></context:component-scan></beans>
TestAnnoWeb:
package com.itheima.g_annotation.b_web;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class TestAnnoWeb {@Testpublic void demo02(){//从spring容器获得String xmlPath = "com/itheima/g_annotation/b_web/beans.xml";ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);StudentAction studentAction = (StudentAction) applicationContext.getBean("studentActionId");studentAction.execute();}}
4.生命周期
初始化:@PostConstruct
销毁:@PreDestroy
5.作用域
@Scope("prototype")多例
UserService:
package com.itheima.g_annotation.c_other;public interface UserService {public void addUser();}
UserServiceImpl:
package com.itheima.g_annotation.c_other;import javax.annotation.PostConstruct;import javax.annotation.PreDestroy;import org.springframework.context.annotation.Scope;import org.springframework.stereotype.Service;@Service("userServiceId")//@Scope("prototype")public class UserServiceImpl implements UserService {@Overridepublic void addUser() {System.out.println("d_scope add user");}@PostConstructpublic void myInit(){System.out.println("初始化");}@PreDestroypublic void myDestroy(){System.out.println("销毁");}}
beans.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="com.itheima.g_annotation.c_other"></context:component-scan></beans>
TestOther:
package com.itheima.g_annotation.c_other;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class TestOther {@Testpublic void demo02(){//spring 工厂String xmlPath = "com/itheima/g_annotation/c_other/beans.xml";ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);UserService userService = applicationContext.getBean("userServiceId" ,UserService.class);UserService userService2 = applicationContext.getBean("userServiceId" ,UserService.class);System.out.println(userService);System.out.println(userService2);applicationContext.close();}}
- spring-装配Bean 基于注解
- spring装配bean(基于注解)
- Spring 装配Bean 基于注解
- Spring-装配Bean 基于注解
- Spring(七)基于注解装配bean
- Spring(3、基于注解装配Bean)
- 装配Bean 基于注解
- bean装配基于注解
- 装配Bean基于注解
- Spring-Bean之基于注解(Annotation)装配解析
- Spring In Action(一):基于注解装配bean
- 基于注解自动装配Bean
- spring注解自动装配Bean
- Spring 使用注解装配Bean
- Spring注解方式装配Bean
- spring实战-注解装配bean
- spring实战-注解装配bean
- Spring Bean装配--@Bean注解注入说明
- 自动导入上传数据的注意事项
- IE6中盒子绝对定位不起效果的解决方法
- python3.5 安装scrapy
- 51Nod - 1639 递推
- POJ 1003.Hangover
- spring装配bean(基于注解)
- 深入浅出讲解:php的socket通信
- Cocos Creator使用小记
- eclipse没有(添加)"Dynamic Web Project"选项的方法
- HDU2044 一只小蜜蜂...
- Leetcode 26. Remove Duplicates from Sorted Array
- 游戏编程中的人工智能技术一书的附录
- ZK设计原理简述
- Oracle监控的关键指标有哪些