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. cat查阅文件技巧

    一.打印除匹配行之外的其它行,使用-v 打印除$和#的注释行:cat file| grep -v ^$ | grep -v ^#

  2. py库:把python打包成exe文件(pyinstaller)

    http://blog.csdn.net/be_quiet_endeavor/article/details/73929077 用Pyinstaller把Python3.4程序打包成可执行文件exe ...

  3. python3 re.compile中含有变量

    id = '7F' reg = re.compile(id + '[^\dA-F]?\d') line = ‘122s 7f 3' match = reg.search(line) 在程序中有时候会遇 ...

  4. spring boot报Unsupported Media Type Content type '*/*;charset=UTF-8' not supported

    1.请求设置Content-Type:application/json即可 ajax一般默认:Content-Type: application/x-www-form-urlencoded;chars ...

  5. mybatis拦截器获取sql

    mybatis获取sql代码 package com.icourt.alpha.log.interceptor; import org.apache.ibatis.executor.Executor; ...

  6. Mybatis运行错误:信息: SQLErrorCodes loaded: [DB2, Derby, H2, HDB, HSQL, Informix, MS-SQL, MySQL, Oracle, PostgreSQL, Sybase]

    Mybatis运行出现错误提示: 五月 23, 2018 12:07:22 上午 org.springframework.jdbc.support.SQLErrorCodesFactory <i ...

  7. 性感天才黑客乔治·霍兹George Hotz 17岁打脸乔布斯20岁搞疯索尼

    1.国内外著名黑客信息 1) 国外著名黑客 George Hotz 乔治·霍兹(George Hotz,1989年10月2日-),美国学生,2007年8月解锁苹果(Apple)iPhone手机,使得i ...

  8. db2开启监控monitor 查看快照snapshot

    ths   https://blog.csdn.net/huaishu/article/details/9671771 #查看监控器 db2 get monitor switches #打开监控器db ...

  9. DJango 基础 (1)

    django基础 知识点: 基本认知 工具准备 新建项目 目录及文件说明 开发服务器 创建视图函数 新建应用(app) 1.基本认知 Django是用Python开发的一个免费开源的Web框架,可以用 ...

  10. Nginx – rewrite 配置 URL重写及301跳转原理图

    Nginx – rewrite 配置 URL重写 官网:http://nginx.org/en/docs/http/ngx_http_rewrite_module.html 语法:rewrite re ...