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();
} }

最新文章

  1. SSTABLE简介
  2. Java 集合 - LinkedList
  3. CodeForces #369 div2 D Directed Roads DFS
  4. 問題排查:F5啟動偵錯後所提示的錯誤 (2)
  5. 记2012-2013年一路的Windows Phone历程
  6. Python中处理时间 —— time模块
  7. ubuntu(16.04.01)学习-day1
  8. JSP页面同时操作所有Input输入框
  9. Python写UTF8文件,UE、记事本打开依然乱码的问题
  10. 程序员之殇 —— (Are you afraid of me? Don&#39;t be.)灵感=神秘感
  11. 手机浏览网页或打开App时莫名弹出支付宝领红包界面的原因及应对措施
  12. JavaScript基础-3
  13. mysqldiff差异比较
  14. Node.js中实现套接字服务
  15. 【python练习题】程序2
  16. pip安装django失败
  17. AngularJS路由系列(2)--刷新、查看路由,路由事件和URL格式,获取路由参数,路由的Resolve
  18. gulp实例
  19. 【bzoj5180】[Baltic2016]Cities 斯坦纳树
  20. http状态码学习笔记

热门文章

  1. nginx ------反向代理和负载均衡
  2. ATS配置自定义日志
  3. python常用库,包网址
  4. RK3288 mipi屏参数配置文件
  5. PAT甲级
  6. ISNUMERIC使用说明和BUG
  7. Nginx 工作原理
  8. Linux下安装Anaconda
  9. git hub 第一篇
  10. 详细分析MySQL事务日志(redo log和undo log) 表明了为何mysql不会丢数据