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基于注解的更多相关文章

  1. 002-Spring4 快速入门-项目搭建、基于注解的开发bean,Bean创建和装配、基于注解的开发bean,Bean初始化销毁、Bean装配,注解、Bean依赖注入

    一.项目搭建 1.项目创建 eclipse→project explorer→new→Project→Maven Project 默认配置即可创建项目 2.spring配置 <dependenc ...

  2. Spring IOC容器装配Bean_基于注解配置方式

    bean的实例化 1.导入jar包(必不可少的) 2.实例化bean applicationContext.xml(xml的写法) <bean id="userDao" cl ...

  3. IOC装配Bean(注解方式)

    Spring的注解装配Bean Spring2.5 引入使用注解去定义Bean @Component 描述Spring框架中Bean Spring的框架中提供了与@Component注解等效的三个注解 ...

  4. 3.装配Bean 基于XML

    一.实例化方式 3种bean实例化方式:默认构造.静态工厂.实例工厂 1.默认构造 <bean id="" class=""> 必须提供默认构造 2 ...

  5. Spring框架(3)---IOC装配Bean(注解方式)

    IOC装配Bean(注解方式) 上面一遍文章讲了通过xml来装配Bean,那么这篇来讲注解方式来讲装配Bean对象 注解方式需要在原先的基础上重新配置环境: (1)Component标签举例 1:导入 ...

  6. spring(读取外部数据库配置信息、基于注解管理bean、DI)

    ###解析外部配置文件在resources文件夹下,新建db.properties(和数据库连接相关的信息) driverClassName=com.mysql.jdbc.Driverurl=jdbc ...

  7. Spring实战2:装配bean—依赖注入的本质

    主要内容 Spring的配置方法概览 自动装配bean 基于Java配置文件装配bean 控制bean的创建和销毁 任何一个成功的应用都是由多个为了实现某个业务目标而相互协作的组件构成的,这些组件必须 ...

  8. spring基于注解进行注入(个人记录)

    spring的Bean基于注解进行配置,再结合自动装配属性,也就DI,其实说白了就相当于初始化的时候给对象赋初值. 配置文件的过程有些麻烦,记录一下. 基于注解进行配置: 1.在application ...

  9. spring实战第二章小记-装配bean

    时间:2020/02/06 一.思想 1.创建应用对象之间协作关系的行为通常称为装配,这也是依赖注入(DI)的本质. 对于上面这句话的个人理解:当我们在new一个对象时如果传入了别的对象作为参数(这个 ...

随机推荐

  1. tensorflow-yolo3系列配置文章汇总

    yolo 网络讲解 https://blog.csdn.net/m0_37192554/article/details/81092514 https://blog.csdn.net/guleileo/ ...

  2. 05python下

    循环loop 有限循环 ,次数限制 无限循环=死循环 continue 结束本次循环,继续下一次循环 break 跳出整个当前的循环 forwhilebreak continue 数据类型 整数 字符 ...

  3. VMware 2017 v14.x 永久许可证激活密钥

    FF31K-AHZD1-H8ETZ-8WWEZ-WUUVA CV7T2-6WY5Q-48EWP-ZXY7X-QGUWD

  4. python3 发送邮件

    import smtplibfrom email.mime.text import MIMETextdef SendEmail(fromAdd,toAdd,subject,text): _pwd = ...

  5. tesseract_vs2015工具包使用

    在vs中配置tesseract4.0: 新建一个空的控制台程序.并找到下图位置 双击.x64.user出现如下图: 单击VC++目录,将tesseract-2015/include/tesseract ...

  6. tensorflow实战讨论

    欢迎关注微信公众号:樱园的玻尔兹曼机

  7. ---dd-wrt memo

    http://blog.csdn.net/fyh2003/article/details/44458657http://blog.csdn.net/u010189241/article/details ...

  8. 关于ES6兼容IE 问题记录之一

    这两天在做前端网页时,遇到一个问题,页面打开发生乱码,如下: 现象:360 浏览器,在急速模式下(即谷歌模式)是OK的显示,第一张图布局OK:在兼容模式下(即IE模式)是显示NG的,第二张图布局乱码 ...

  9. Sql Server数据库之四个增删改查

    一.数据库的增删改查 1.新建数据库 create database students on primary ( name="students_data",--主数据文件的逻辑名 ...

  10. Python中的logging模块【转】https://www.cnblogs.com/yelin/p/6600325.html

    [转]https://www.cnblogs.com/yelin/p/6600325.html 基本用法 下面的代码展示了logging最基本的用法. 1 # -*- coding: utf-8 -* ...