一、通用注解

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. 淘宝账号基于OAuth2.0的登录验证授权登陆第三方网站

    首先得有一个注册的appkey和App Secret   该流程分三个步骤: 第一步:通过用户授权获取授权码Code: 第二步:用上一步获取的Code和应用密钥(AppSecret)通过Https P ...

  2. ZooKeeper学习第五期--ZooKeeper管理分布式环境中的数据

    引言 本节本来是要介绍ZooKeeper的实现原理,但是ZooKeeper的原理比较复杂,它涉及到了paxos算法.Zab协议.通信协议等相关知识,理解起来比较抽象所以还需要借助一些应用场景,来帮我们 ...

  3. pandas 修改 DataFrame 列名

    问题: 有一个DataFrame,列名为:['$a', '$b', '$c', '$d', '$e'] 现需要改为:['a', 'b', 'c', 'd', 'e'] 有何办法? import pan ...

  4. ace布置小作业: 制作一个简单的电话号码归属地查询软件:JSON解析和Volly发送get请求

    大概就这个样子 用到JSON解析和Volly发送Get请求两个知识点 关于Volly的用法请看我的这篇: http://www.cnblogs.com/AceIsSunshineRain/p/5177 ...

  5. 信息安全系统设计基础exp_5

    北京电子科技学院(BESTI) 实     验    报     告 课程:信息安全系统设计基础 班级:1353 姓名:郑伟.吴子怡 学号:20135322.20135313 指导教师: 娄嘉鹏 实验 ...

  6. Spring 集成 Hibernate 和 Struts 2

    在Spring中集成Hibernate,实际上就是将Hibernate中用到的数据源DataSource. SessionFactory实例(通常使用Hibernate访问数据库时,应用程序会先创建S ...

  7. php模式设计之 单例模式

    模式设计是什么?初学者一开始会被这高大上的名称给唬住.而对于有丰富编程经验的老鸟来说,模式设计又是无处不在.很多接触的框架就是基于各种模式设计形成的. 简单说,在写代码的过程中一开始往往接触的是面向过 ...

  8. 如何优雅的写一篇安利文-以Sugar ORM为例

    前言 我最近喜欢把写的十分优美的技术文章叫做安利文.首先,文章必须是原创而非软广:其次,阅读之后不仅能快速吸纳技术要点并入门开发,还能感同身受的体会作者热情洋溢的赞美和急于分享心得体验的心情,让人感觉 ...

  9. [软件测试]网站压测工具Webbench源码分析

    一.我与webbench二三事 Webbench是一个在linux下使用的非常简单的网站压测工具.它使用fork()模拟多个客户端同时访问我们设定的URL,测试网站在压力下工作的性能.Webbench ...

  10. Git.Framework 框架随手记--准备工作

    前面已经提到过了本框架的由来,时至今日该框架已经和最初版本有了天壤之别.因为仍有部分代码是采用原有的框架,所以本框架也算不上原创,只是在原有的基础上不断的改进,所以希望了解此框架的人不要过多的指责. ...