spring 注解实例
先不说网上的那些例子了,百度到的都是一些零碎的东西。我之所以记博客,除了总结之外,很大一个原因是对网上的某些东西真的很无语。
拿注解来说,什么入门实例的东西,说是入门,却连一个基本的hello world 都没有,呵呵。
之前一直都是用xml配置,注解现在用的也多了,要好好看看。
本篇里面都是基础,代码清单都会列全。
首先是引入spring包,这里用的是maven,pom.xml加入:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.0.6.RELEASE</version>
</dependency>
然后maven install,引入包。
接着,xml的配置文件,这里包括头文件,以及注解需要的配置:
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-3.0.xsd"> <context:annotation-config></context:annotation-config>
<context:component-scan base-package="com.spring.ioc"></context:component-scan>
</beans>
好了,从现在开始。
代码结构:

Man包下是第二个例子。
先说第一个例子,无接口的。
person.java:
package com.spring.ioc; import org.springframework.stereotype.Component; @Component
public class Person {
private String name;
private String sex; public Person() {
name="wang";
sex="man";
}
/* public Person(String name, String sex) {
super();
name="wang";
sex="man";
}*/
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
} }
里面初始化了一些数据,作为一个bean。
depart.java:
package com.spring.ioc; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; @Component
public class Depart { @Autowired
private Person person; public String getDepart(){
String s=person.getName()+" in depart";
return s;
}
}
这个是为了演示,在depart里面注入person。
主类测试用的:
package com.spring.ioc; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main { public static void main(String[] args) {
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("beans.xml");
Depart depart=(Depart) applicationContext.getBean("depart");
System.out.println(depart.getDepart());
}
}
运行后,结果:
wang in depart
第二个例子,带有接口的例子:
创建接口,man:
package com.spring.ioc.Man;
public interface Man {
public String say();
}
然后有两个实现类:
package com.spring.ioc.Man; import org.springframework.stereotype.Component; @Component
public class Chinese implements Man { public String say() {
// TODO Auto-generated method stub
return "你好";
} }
package com.spring.ioc.Man; import org.springframework.stereotype.Component; @Component
public class American implements Man { public String say() {
// TODO Auto-generated method stub
return "hello";
} }
然后创建一个类,注入这两个接口实现类。
package com.spring.ioc.Man; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component; @Component
public class ManService {
@Autowired
@Qualifier(value="chinese")
private Man man; public String sayChineseHello(){
return man.say()+",欢迎";
}
@Autowired
@Qualifier(value="american")
private Man aman;
public String sayEnglishHello(){
return aman.say()+",welcome";
}
}
主类:
package com.spring.ioc.Man; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main { public static void main(String[] args) {
ApplicationContext context=new ClassPathXmlApplicationContext("beans.xml");
ManService manService=(ManService) context.getBean("manService");
String string=manService.sayChineseHello();
System.out.println(string);
System.out.println(manService.sayEnglishHello());
}
}
运行结果:
你好,欢迎
hello,welcome
关于接口的,要在实现类上面添加注解说明。坑爹的,网上有篇文章说是要在接口上添加注解,不能在实现类上面,导致错误了半天。
关于注解的各个标签,可以单独百度一下,很多讲解。
spring 注解实例的更多相关文章
- Spring注解实例
public class ActivityAction extends CoreAction { private static final Logger log = Logger.getLogger( ...
- Spring Aop实例@Aspect、@Before、@AfterReturning@Around 注解方式配置
用过spring框架进行开发的人,多多少少会使用过它的AOP功能,都知道有@Before.@Around和@After等advice.最近,为了实现项目中的输出日志和权限控制这两个需求,我也使用到了A ...
- Spring的AOP配置文件和注解实例解析
1.1 Spring的AOP配置文件和注解实例解析 AOP它利用一种称为"横切"的技术,将那些与核心业务无关,却为业务模块所共同调用的逻辑或责任封装起来,便于减 ...
- spring注解说明之Spring2.5 注解介绍(3.0通用)
spring注解说明之Spring2.5 注解介绍(3.0通用) 注册注解处理器 方式一:bean <bean class="org.springframework.beans.fac ...
- 使用Spring注解来简化ssh框架的代码编写
目的:主要是通过使用Spring注解的方式来简化ssh框架的代码编写. 首先:我们浏览一下原始的applicationContext.xml文件中的部分配置. <bean id="m ...
- [转]Spring 注解总结
原文地址:http://blog.csdn.net/wangshfa/article/details/9712379 一 注解优点?注解解决了什么问题,为什么要使用注解? 二 注解的来龙去脉(历史) ...
- spring 注解简单使用
一.通用注解 1.项目结构: 2.新建Person类,注解@Component未指明id,则后期使用spring获取实例对象时使用默认id="person"方式获取或使用类方式获取 ...
- Spring 注解总结
声明:这是转载的.内容根据网上资料整理.相关链接:http://www.360doc.com/content/10/1118/16/2371584_70449913.shtmlhttp://www.i ...
- Spring Security4实例(Java config版)——ajax登录,自定义验证
本文源码请看这里 相关文章: Spring Security4实例(Java config 版) -- Remember-Me 首先添加起步依赖(如果不是springboot项目,自行切换为Sprin ...
随机推荐
- PR合并回写
) as LGORT ,'SAPRFC' as ERNAM,out_pr.due_datetime,out_pr.so_id,out_pr.so_lineid,out_pr.sobsl from V_ ...
- python使用外部PY文件的变量
在用python和selenium编写登录等脚本时,一直都是给用户名和密码直接赋值.但是考虑到这样不便于管理,而且可能多个地方用到同一个变量,所以想把变量放在一个单独的文件中进行管理. 以登录脚本为例 ...
- Idea多个module下maven的pom.xml失效的问题
今天在Idea中配置spring-cloud时,配置了两个module,结果其中一个module的pom.xml失效了.. 解决方法: 1.点击Idea右侧的Maven Project 2.点击&qu ...
- python的线程锁
1.先看一个例子,一个进程可以起多个线程,多个线程都共享这个线程的内存 import threading import time num = 100 thread_lock = threading.L ...
- 二叉树的层次遍历 · Binary Tree Level Order Traversal
[抄题]: 给出一棵二叉树,返回其节点值的层次遍历(逐层从左往右访问) [思维问题]: [一句话思路]: 用queue存每一层 [输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况 ...
- 在IE10下,DropDownList的AutoPostBack不能触发
Default.aspx 文件 <%@ Page Language="C#" AutoEventWireup="true" CodeFile=" ...
- 表单提交的两种请求方式:post与get。post与get两者的对比分析
post与get两者的对比分析:
- Spring框架之演示JDBC的模板类
1. 步骤一:创建数据库的表结构 create database spring_day03; use spring_day03; create table t_account( id int prim ...
- Django之virtualenv下安装xadmin
1.安装xadmin,通过pip 进入virtualenv pip安装xadmin pyyuc:~ yuchao$ source PycharmProjects/mxvenv/bin/activate ...
- connect strings sql server
https://www.connectionstrings.com/sql-server/ Server=myServerAddress[,port];Database=myDataBase;User ...