spring 注解简单使用
一、通用注解
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>
spring 注解简单使用的更多相关文章
- spring注解简单记录
@Autowired 自动匹配,按类型 @qualifiter("beanname") 当有多个bean匹配时,可指定bean名称 @Resource byname优先匹配,然后b ...
- spring注解开发中常用注解以及简单配置
一.spring注解开发中常用注解以及简单配置 1.为什么要用注解开发:spring的核心是Ioc容器和Aop,对于传统的Ioc编程来说我们需要在spring的配置文件中邪大量的bean来向sprin ...
- Spring boot 注解简单备忘
Spring boot 注解简单备忘 1.定义注解 package com.space.aspect.anno;import java.lang.annotation.*; /** * 定义系统日志注 ...
- Spring Aop(二)——基于Aspectj注解的Spring Aop简单实现
转发地址:https://www.iteye.com/blog/elim-2394762 2 基于Aspectj注解的Spring Aop简单实现 Spring Aop是基于Aop框架Aspectj实 ...
- spring注解源码分析--how does autowired works?
1. 背景 注解可以减少代码的开发量,spring提供了丰富的注解功能.我们可能会被问到,spring的注解到底是什么触发的呢?今天以spring最常使用的一个注解autowired来跟踪代码,进行d ...
- Spring cache简单使用guava cache
Spring cache简单使用 前言 spring有一套和各种缓存的集成方式.类似于sl4j,你可以选择log框架实现,也一样可以实现缓存实现,比如ehcache,guava cache. [TOC ...
- Spring注解
AccountController .java Java代码 1. /** 2. * 2010-1-23 3. */ 4. packag ...
- spring 注解的优点缺点
注解与XML配置的区别 注解:是一种分散式的元数据,与源代码耦合. xml :是一种集中式的元数据,与源代码解耦. 因此注解和XML的选择上可以从两个角度来看:分散还是集中,源代码耦合/解耦. 注解的 ...
- spring注解scheduled实现定时任务
只想说,spring注解scheduled实现定时任务使用真的非常简单. 一.配置spring.xml文件 1.在beans加入xmlns:task="http://www.springfr ...
随机推荐
- Css 特殊或不常用属性
1. -webkit-font-smoothing: antialiased; CSS3中用于webkit引擎(如chrome)中设置字体的抗锯齿或者说光滑度的属性.有3个属性:none用于小像素的文 ...
- High Performance Animations
http://www.html5rocks.com/zh/tutorials/speed/high-performance-animations/
- no2.crossdomain.xml批量读取(待完善)
读取太多url有问题 #coding=utf-8 import urllib import requests import sys import re import time def getxml(u ...
- php基础19:文件
<?php //1.打开文件的更好的方法是通过 fopen() 函数.此函数为您提供比 readfile() 函数更多的选项. //fopen() 的第一个参数包含被打开的文件名,第二个参数规定 ...
- LeetCode:Climbing Stairs(编程之美2.9-斐波那契数列)
题目链接 You are climbing a stair case. It takes n steps to reach to the top. Each time you can either c ...
- Spring Cache和MyBatis的使用
1.http://www.importnew.com/22757.html spring chache缓存的使用. 2.http://www.importnew.com/22783.html ...
- Java实现文件的加密与解密
最近在做一个项目,需要将资源文件(包括图片.动画等类型)进行简单的加密后再上传至云上的服务器,而在应用程序中对该资源使用前先将读取到的文件数据进行解密以得到真正的文件信息.此策略的原因与好处是将准备好 ...
- 四则运算 Day2
元旦快乐篇 别人在过元旦,而我却在敲代码,说多了都是泪. 设计思路 1. 界面设计 程序运行时,跳出运行说明提示用户如何操作 用户阅读完说明后点击开始进入主界面,即操作界面,操作界面分为计时区,操作区 ...
- github的初次体验及管理代码的心得
周六早上的课上,助教给我们演示了一遍如何上传和下载代码库,新建代码库等等,但是是在linux上的,而我的笔记本的操作系统是win7的.而在教室中的尝试因为网络原因,虽然可以上github的网站,但是下 ...
- tomcat虚拟路径的几种配置方法
一般我们都是直接引用webapps下面的web项目,如果我们要部署一个在其它地方的WEB项目,这就要在TOMCAT中设置虚拟路径了,Tomcat的加载web顺序是先加载 $Tomcat_home$\c ...