6.装配Bean基于注解
1.注解:就是一个类,使用@注解名称
开发中:使用注解 取代 xml配置文件。
@Component取代<bean class="">
@Component("id") 取代 <bean id="" class="">
2.web开发,提供3个@Component注解衍生注解(功能一样)取代<bean class="">
@Repository :dao层 @Service:service层 @Controller:web层
3.依赖注入 ,给私有字段设置,也可以给setter方法设置
普通值:@Value("")
引用值:
方式1:按照【类型】注入
@Autowired
方式2:按照【名称】注入1
@Autowired
@Qualifier("名称")
方式3:按照【名称】注入2
@Resource("名称")
4.生命周期
初始化:@PostConstruct
销毁:@PreDestroy
5.作用域
@Scope("prototype") 多例
注解使用前提,添加命名空间,让spring扫描含有注解类

例子一:
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.jd.annotation.ioc"></context:component-scan>
</beans>
UserService.java
package com.jd.annotation.ioc; /**
* @author weihu
* @date 2018/8/14/014 0:16
*/
public interface UserService {
void addUser();
}
UserServiceImpl.java
package com.jd.annotation.ioc; import org.springframework.stereotype.Component; /**
* @author weihu
* @date 2018/8/14/014 0:16
*
* @Component("id") 取代 <bean id="" class="">
*/
@Component("userServiceId")
public class UserServiceImpl implements UserService {
@Override
public void addUser() {
System.out.println("添加用户成功!");
}
}
TestAnnoIoC.java
package com.jd.annotation.ioc; import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; /**
* @author weihu
* @date 2018/8/14/014 0:19
*/
public class TestAnnoIoC { @Test
public void testAnnotitaion(){
String xmlPath="com/jd/annotation/ioc/beans.xml";
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
UserService userService = (UserService) applicationContext.getBean("userServiceId");
userService.addUser();
}
}
例子二:
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.jd.annotation.web"></context:component-scan>
</beans>
StudentAction.java
package com.jd.annotation.web; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller; /**
* @author weihu
* @date 2018/8/14/014 0:25 controller层
*/
@Controller("studentActionId")
public class StudentAction { @Autowired //默认按照类型
private StudentService studentService; public void execute(){
studentService.addStudent();
}
}
StudentDao.java
package com.jd.annotation.web; /**
* @author weihu
* @date 2018/8/14/014 0:28
*/
public interface StudentDao {
void save();
}
StudentDaoImpl.java
package com.jd.annotation.web;
import org.springframework.stereotype.Repository; /**
* @author weihu
* @date 2018/8/14/014 0:29
*
* Repository :dao层
*/
@Repository("studentDaoId")
public class StudentDaoImpl implements StudentDao{
@Override
public void save() {
System.out.println("save dao");
}
}
StudentService.java
package com.jd.annotation.web; /**
* @author weihu
* @date 2018/8/14/014 0:26
*/
public interface StudentService {
void addStudent();
}
StudentServiceImpl.java
package com.jd.annotation.web; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service; /**
* @author weihu
* @date 2018/8/14/014 0:27 service层
*/
@Service
public class StudentServiceImpl implements StudentService { private StudentDao studentDao; @Autowired
@Qualifier("studentDaoId")
public void setStudentDao(StudentDao studentDao) {
this.studentDao = studentDao;
} @Override
public void addStudent() {
studentDao.save(); }
}
TestAnnoWeb.java
package com.jd.annotation.web; import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; /**
* @author weihu
* @date 2018/8/14/014 0:44
*/
public class TestAnnoWeb {
@Test
public void testAnnotion(){
String xmlPath="com/jd/annotation/web/beans.xml";
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
StudentAction studentAction = applicationContext.getBean("studentActionId", StudentAction.class);
studentAction.execute();
}
}
例子三:
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.jd.annotation.other"></context:component-scan>
</beans>
UserService.java
package com.jd.annotation.other;
public interface UserService {
public void addUser();
}
UserServiceImpl.java
package com.jd.annotation.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 { @Override
public void addUser() {
System.out.println("d_scope add user");
} @PostConstruct
public void myInit(){
System.out.println("初始化");
}
@PreDestroy
public void myDestroy(){
System.out.println("销毁");
} }
TestOther.java
package com.jd.annotation.other; import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class TestOther { @Test
public void testBeforeAndAfter(){
//spring 工厂
String xmlPath = "com/jd/annotation/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();
} }
6.装配Bean基于注解的更多相关文章
- 002-Spring4 快速入门-项目搭建、基于注解的开发bean,Bean创建和装配、基于注解的开发bean,Bean初始化销毁、Bean装配,注解、Bean依赖注入
一.项目搭建 1.项目创建 eclipse→project explorer→new→Project→Maven Project 默认配置即可创建项目 2.spring配置 <dependenc ...
- Spring IOC容器装配Bean_基于注解配置方式
bean的实例化 1.导入jar包(必不可少的) 2.实例化bean applicationContext.xml(xml的写法) <bean id="userDao" cl ...
- IOC装配Bean(注解方式)
Spring的注解装配Bean Spring2.5 引入使用注解去定义Bean @Component 描述Spring框架中Bean Spring的框架中提供了与@Component注解等效的三个注解 ...
- 3.装配Bean 基于XML
一.实例化方式 3种bean实例化方式:默认构造.静态工厂.实例工厂 1.默认构造 <bean id="" class=""> 必须提供默认构造 2 ...
- Spring框架(3)---IOC装配Bean(注解方式)
IOC装配Bean(注解方式) 上面一遍文章讲了通过xml来装配Bean,那么这篇来讲注解方式来讲装配Bean对象 注解方式需要在原先的基础上重新配置环境: (1)Component标签举例 1:导入 ...
- spring(读取外部数据库配置信息、基于注解管理bean、DI)
###解析外部配置文件在resources文件夹下,新建db.properties(和数据库连接相关的信息) driverClassName=com.mysql.jdbc.Driverurl=jdbc ...
- Spring实战2:装配bean—依赖注入的本质
主要内容 Spring的配置方法概览 自动装配bean 基于Java配置文件装配bean 控制bean的创建和销毁 任何一个成功的应用都是由多个为了实现某个业务目标而相互协作的组件构成的,这些组件必须 ...
- spring基于注解进行注入(个人记录)
spring的Bean基于注解进行配置,再结合自动装配属性,也就DI,其实说白了就相当于初始化的时候给对象赋初值. 配置文件的过程有些麻烦,记录一下. 基于注解进行配置: 1.在application ...
- spring实战第二章小记-装配bean
时间:2020/02/06 一.思想 1.创建应用对象之间协作关系的行为通常称为装配,这也是依赖注入(DI)的本质. 对于上面这句话的个人理解:当我们在new一个对象时如果传入了别的对象作为参数(这个 ...
随机推荐
- Android 开发 框架系列 百度语音合成
官方文档:http://ai.baidu.com/docs#/TTS-Android-SDK/6d5d6899 官方百度语音合成控制台:https://cloud.baidu.com/product/ ...
- jsp jstl quote symbol expected
org.apache.jasper.JasperException: /WEB-INF/jsp/user/index.jsp (line: 2, column: 27) quote symbol ex ...
- 【学习】基础知识:数组和矢量计量【Numpy】
Numpy是高性能科学计算和数据分析的基础包.功能如下: ndarray 一个具有矢量算法运算和复杂广播能力的快速且节省空间的多维数组 用于对整组数据进行快速运算的标准数学函数(无需编写循环) 用于读 ...
- python学习笔记_week28
heap import heapq import random heap = [] data = list(range(10000)) random.shuffle(data) # for num i ...
- xxx.jar 中没有主清单属性
springboot 中是可以通过 jar 将整个项目打包成一个fat jar 的, 这个大家都知道. <!-- 打包jar文件时,配置manifest文件,加入lib包的jar依赖 --&g ...
- python大法好——递归、内置函数、函数进阶
1.递归(自己干自己) def fun(n): n=n/2 print(n) if(n>2) fun(n) #函数调用 fun(10) 结果是5 2 1 0 递归的执行过程:递归执行到最里面一层 ...
- Echarts报错 Can't read property 'getWidth' of null
统计图报错: 这里的报错与echarts无关,与zrender有关,zrender是echarts依赖的canvas绘图库 你不需要了解zrender,这个问题是你代码出了错 谨记::代码的错
- spring-security使用
极客学院Spring Security 例子 <?xml version="1.0" encoding="UTF-8"?> <beans:be ...
- django内置分页功能扩展
实现自定制页码数类型class myPaginator(Paginator): def __init__(self,curr_page,per_page_num,*args,**kwargs): se ...
- Django_Form验证(二),ajax验证
还是一个简单的html提交页面,ajax提交就不需要form表单了: <p><input id="a" type="text" name=&q ...