一、通用注解

1、项目结构:

2、新建Person类,注解@Component未指明id,则后期使用spring获取实例对象时使用默认id="person"方式获取或使用类方式获取

package hjp.spring.annotation.commen;

import org.springframework.stereotype.Component;

//@Component
@Component("personId")
public class Person {
private String name;
private Integer age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + "]";
}
}

Person

3、新建beans.xml文件,相比之前配置多了

xmlns:context="http://www.springframework.org/schema/context"

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
不再使用bean节点配置,而是使用context:component-scan节点,属性base-package指明要扫描注解的包
<?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">
<!-- <bean id="personId" class="hjp.spring.annotation.commen.Person"></bean> -->
<context:component-scan base-package="hjp.spring.annotation.commen"></context:component-scan>
</beans>

4、新建测试类

package hjp.spring.annotation.commen;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component; public class TestApp {
@Test
public void demo1() {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
"hjp/spring/annotation/commen/beans.xml");
//注解未指定id时,默认id为person
//Person person = applicationContext.getBean("person", Person.class);
//注解指定id,即@Component("personId")时
//Person person = applicationContext.getBean("personId",Person.class);
//不管是否指定id,使用类方式获取实例都可以
Person person = applicationContext.getBean(Person.class);
System.out.println(person);
}
}

测试类

二、衍生三层开发注解

@Controller 修饰Web层;@Service 修饰service层;@Repository 修饰dao层

依赖注入:方式1、普通数据 @Value

       方式2、引用数据 @Autowired,默认按照类型进行注入;如果想要按照名称进行注入,还需要使用注解@Qualifier("名称")

1、项目结构

2、新建UserDao类

package hjp.spring.annotation.web;

import org.springframework.stereotype.Repository;

//@Repository
@Repository("userDaoId")
public class UserDao {
public void save() {
System.out.println("add user");
}
}

UserDao

3、新建UserService类

package hjp.spring.annotation.web;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service; @Service
public class UserService {
@Autowired
//@Qualifier("userDaoId") //指向UserDao类注解@Repository("userDaoId")指定的Id
//属性注入也可以使用注解@Resource
//@Resource
private UserDao userDao;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
} public void addUser() {
userDao.save();
}
}

UserService

4、新建UserAction类

package hjp.spring.annotation.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller; @Controller("userActionId")
//@Scope("prototype")//多例
public class UserAction {
@Autowired
private UserService userService; public void setUserService(UserService userService) {
this.userService = userService;
}
public void execute() {
userService.addUser();
}
}

UserAction

5、新建测试类

package hjp.spring.annotation.web;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class TestApp {
@Test
public void demo1() {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
"hjp/spring/annotation/web/beans.xml");
UserAction userAction = applicationContext.getBean("userActionId", UserAction.class);
System.out.println(userAction);//用于测试单例和多例
userAction.execute();
userAction=applicationContext.getBean("userActionId", UserAction.class);
System.out.println(userAction);//用于测试单例和多例
}
}

测试类

6、新建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">
<!-- <bean id="personId" class="hjp.spring.annotation.commen.Person"></bean> -->
<!-- 让spring对含有注解的类进行扫描 -->
<context:component-scan base-package="hjp.spring.annotation.web"></context:component-scan>
</beans>

beans.xml

三、其他注解:如@PostConstruct修饰初始化;@PreDestroy修饰销毁

四、项目中对XML和注解的使用情况

1、纯XML,整合第三方(jar包)

2、纯注解,限制条件必须有源码,简化代码开发

3、xml+注解,xml配置bean,代码中使用注入注解

如果混合使用不需要扫描,只需要加入<context:annotation-config></context:annotation-config>配置

测试Demo可以将UserDao类的@Repository、UserService类的@Service、UserAction类的@Controller去掉,只保留注入注解,

然后修改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"> <!-- 只使用注入的注解 -->
<bean id="userDaoId" class="hjp.spring.annotation.web.UserDao"></bean>
<bean id="userServiceId" class="hjp.spring.annotation.web.UserService"></bean>
<bean id="userActionId" class="hjp.spring.annotation.web.UserAction"></bean>
<context:annotation-config></context:annotation-config>
</beans>

最新文章

  1. ACM:SCU 4437 Carries - 水题
  2. nhibernat4.0.0.4000 bug
  3. Mac 安装mysql
  4. oc实例变量初始化方法
  5. 做bbs论坛项目的收获(1)
  6. 三分初练QAQ
  7. The 7 Stages Of Scaling Web Apps--reference
  8. codeforces 671C Ultimate Weirdness of an Array 线段树+构造
  9. xml约束DTD演示
  10. Windows2012中安装域控(DC) + SQL Server 2014 + TFS 2015
  11. IIS6.0服务器搭建网站无法访问解决方法
  12. c语言指向结构体数组的指针
  13. Android5.0L下因sensorservice crash导致systemserver重新启动的第二种场景分析
  14. Intent的概念及应用(二)
  15. 微信小程序简单入门1
  16. JS获取fileupload文件全路径
  17. Dynamics CRM 2015 站点地图公告配置实体显示名称的变更
  18. How to download the installation package by ZOL Downer
  19. python爬虫之scrapy
  20. ViewPager+Fragment切换卡顿解决办法

热门文章

  1. 【C#】WM 消息大全
  2. Google play billing(Google play 内支付)
  3. XML CDATA的作用
  4. Lenovo GTX960M 配置CUDA
  5. 【转】Python Twisted介绍
  6. 20135316王剑桥 linux第二周课实验笔记
  7. Android--手持PDA读取SD卡中文件
  8. OpenGLES 2.0 可编程渲染管线
  9. C#中的yield return与Unity中的Coroutine(协程)(上)
  10. [USACO2006][poj3182]The Grove(巧妙的BFS)