一、通用注解

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 注解简单使用的更多相关文章

  1. spring注解简单记录

    @Autowired 自动匹配,按类型 @qualifiter("beanname") 当有多个bean匹配时,可指定bean名称 @Resource byname优先匹配,然后b ...

  2. spring注解开发中常用注解以及简单配置

    一.spring注解开发中常用注解以及简单配置 1.为什么要用注解开发:spring的核心是Ioc容器和Aop,对于传统的Ioc编程来说我们需要在spring的配置文件中邪大量的bean来向sprin ...

  3. Spring boot 注解简单备忘

    Spring boot 注解简单备忘 1.定义注解 package com.space.aspect.anno;import java.lang.annotation.*; /** * 定义系统日志注 ...

  4. Spring Aop(二)——基于Aspectj注解的Spring Aop简单实现

    转发地址:https://www.iteye.com/blog/elim-2394762 2 基于Aspectj注解的Spring Aop简单实现 Spring Aop是基于Aop框架Aspectj实 ...

  5. spring注解源码分析--how does autowired works?

    1. 背景 注解可以减少代码的开发量,spring提供了丰富的注解功能.我们可能会被问到,spring的注解到底是什么触发的呢?今天以spring最常使用的一个注解autowired来跟踪代码,进行d ...

  6. Spring cache简单使用guava cache

    Spring cache简单使用 前言 spring有一套和各种缓存的集成方式.类似于sl4j,你可以选择log框架实现,也一样可以实现缓存实现,比如ehcache,guava cache. [TOC ...

  7. Spring注解

    AccountController .java Java代码   1.        /** 2.         * 2010-1-23 3.         */ 4.        packag ...

  8. spring 注解的优点缺点

    注解与XML配置的区别 注解:是一种分散式的元数据,与源代码耦合. xml :是一种集中式的元数据,与源代码解耦. 因此注解和XML的选择上可以从两个角度来看:分散还是集中,源代码耦合/解耦. 注解的 ...

  9. spring注解scheduled实现定时任务

    只想说,spring注解scheduled实现定时任务使用真的非常简单. 一.配置spring.xml文件 1.在beans加入xmlns:task="http://www.springfr ...

随机推荐

  1. 查询Sqlserver数据库死锁的一个存储过程(转)

    链接 :http://www.cnblogs.com/mzhanker/archive/2011/06/04/2072739.html 使用sqlserver作为数据库的应用系统,都避免不了有时候会产 ...

  2. usb驱动开发2之代码地图

    USB只是Linux庞大家族里的一个小部落,host controller是它们的族长,族里的每个USB设备都需要被系统识别.下图显示包含一个USB接口的USB鼠标导出的结果. USB系统中的第一个U ...

  3. cobbler

    原理: http://www.cnblogs.com/mchina/p/centos-pxe-kickstart-auto-install-os.html 一键脚本 http://tshare365. ...

  4. wireshark添加ip.id字段

    wireshark添加ip.id字段 为了在多个设备上追踪同一个数据包. 如果是同一个会话,则可以计算延迟, 如sta和应用服务器慢,这种问题,可以根据这个加上ip.id追踪数据到哪里慢了.    

  5. 【Mysql】 my.ini配置一例

    # For advice on how to change settings please see # http://dev.mysql.com/doc/refman/5.6/en/server-co ...

  6. 在matlab中进行地理坐标和像素坐标的相互转换

    clc;close all;clear; %地理坐标和像素坐标的相互转换 [pic,R]=geotiffread('boston.tif'); %读取带地理坐标信息的tif影像 [m,n,~]=siz ...

  7. 创建Maven工程

    一.Maven工程创建 File->New->Other,进入: 点击Next,进入: 勾选上Create a simple project(不使用骨架) 点击Next,进入: 输入项目名 ...

  8. polya计数定理在ACM-icpc中的应用

    [数学公式] PG(x1,x2,...,xn) = 1/|G| * ∑π∈G x1^b1 * x2^b2*...*bn^bn   其中π是1^b12^b2...n^bn型轮换 然后一般染色情况下x1= ...

  9. HTML5 实现橡皮擦的擦除效果

    声明:本文为原创文章,如需转载,请注明来源WAxes,谢谢! 最近项目刚好用到这种效果,也就是有点像刮刮卡一样,在移动设备上,把某张图片刮掉显示出另一张图片.效果图如下:  DEMO请戳右:DEMO ...

  10. Bootstrap系列 -- 36. 向上弹起的下拉菜单

    有些菜单是需要向上弹出的,比如说你的菜单在页面最底部,而这个菜单正好有一个下拉菜单,为了让用户有更好的体验,不得不让下拉菜单向上弹出.在Bootstrap框架中专门为这种效果提代了一个类名“dropu ...