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. Android 开发 知晓各种id信息 获取线程ID、activityID、内核ID

    /** * Returns the identifier of this process's user. * 返回此进程的用户的标识符. */ Log.e(TAG, "Process.myU ...

  2. [UnityAPI]SerializedObject类 & SerializedProperty类

    以Image类为例 1.MyImage.cs using UnityEngine; using UnityEngine.UI; public class MyImage : Image { ; pro ...

  3. This iPhone 6s is running iOS 11.3.1 (15E302), which may not be supported by this version of Xcode.

    This iPhone 6s is running iOS 11.3.1 (15E302), which may not be supported by this version of Xcode.

  4. django 数据库查询 ORM

    实用的logging模块: zaisetting配置中加入下列配置,将sql语句打印到屏幕上,可以进行查看. LOGGING = { 'version': 1, 'disable_existing_l ...

  5. js 模拟css3 动画

    <html> <head> <title> javaScript缓动入门 </title> </head> <body> < ...

  6. idea git 整合使用

    1.首先在github网站上新建一个repository 打开https://github.com/ 找到new repository按钮 输入repository name 选择public 记录下 ...

  7. 大数据入门到精通13--为后续和MySQL数据库准备

    We will be using the sakila database extensively inside the rest of the course and it would be great ...

  8. 原子性: Interlocked 类

    public class CounterNoLock:CountBase { private int _count; public int Count { get { return _count; } ...

  9. python 迭代多个对象

    并行迭代 zip for a,b,c in zip(list,list,tuple,list): print a,b,c 串行迭代 itertools.chain a = [1,2,3,4,5] b ...

  10. Metasploit用法大全

    Metasploit用户接口msfconsoleArmitage:  KaliGUI启动:armitage命令启动 Metasploit功能程序msfvenom集成了载荷生成器.载荷编码器.空指令生成 ...