先不说网上的那些例子了,百度到的都是一些零碎的东西。我之所以记博客,除了总结之外,很大一个原因是对网上的某些东西真的很无语。

拿注解来说,什么入门实例的东西,说是入门,却连一个基本的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 注解实例的更多相关文章

  1. Spring注解实例

    public class ActivityAction extends CoreAction { private static final Logger log = Logger.getLogger( ...

  2. Spring Aop实例@Aspect、@Before、@AfterReturning@Around 注解方式配置

    用过spring框架进行开发的人,多多少少会使用过它的AOP功能,都知道有@Before.@Around和@After等advice.最近,为了实现项目中的输出日志和权限控制这两个需求,我也使用到了A ...

  3. Spring的AOP配置文件和注解实例解析

    1.1           Spring的AOP配置文件和注解实例解析 AOP它利用一种称为"横切"的技术,将那些与核心业务无关,却为业务模块所共同调用的逻辑或责任封装起来,便于减 ...

  4. spring注解说明之Spring2.5 注解介绍(3.0通用)

    spring注解说明之Spring2.5 注解介绍(3.0通用) 注册注解处理器 方式一:bean <bean class="org.springframework.beans.fac ...

  5. 使用Spring注解来简化ssh框架的代码编写

     目的:主要是通过使用Spring注解的方式来简化ssh框架的代码编写. 首先:我们浏览一下原始的applicationContext.xml文件中的部分配置. <bean id="m ...

  6. [转]Spring 注解总结

    原文地址:http://blog.csdn.net/wangshfa/article/details/9712379 一 注解优点?注解解决了什么问题,为什么要使用注解? 二 注解的来龙去脉(历史) ...

  7. spring 注解简单使用

    一.通用注解 1.项目结构: 2.新建Person类,注解@Component未指明id,则后期使用spring获取实例对象时使用默认id="person"方式获取或使用类方式获取 ...

  8. Spring 注解总结

    声明:这是转载的.内容根据网上资料整理.相关链接:http://www.360doc.com/content/10/1118/16/2371584_70449913.shtmlhttp://www.i ...

  9. Spring Security4实例(Java config版)——ajax登录,自定义验证

    本文源码请看这里 相关文章: Spring Security4实例(Java config 版) -- Remember-Me 首先添加起步依赖(如果不是springboot项目,自行切换为Sprin ...

随机推荐

  1. MySQL8 重置改root密码及开放远程访问

    1. 修改配置文件 先修改配置文件:vim /etc/my.conf 在 [mysqld] 下加上下面这行 skip-grant-tables 重启 mysql 服务: service mysqld ...

  2. 项目中调试SQLServer 方便的查看SQL语句的执行时间的方法

    第一种方法,先记录执行前的时间,然后在记录执行Sql后的时间,然后做减法 1 第一种方法: 2 declare @begin_date datetime 3 declare @end_date dat ...

  3. springmvc DispatchServlet初始化九大加载策略(二)

    4. initHandlerMappings 请求分发 HandlerMappings是一个List<HandlerMapping>类型数据,也就是说初始化可以存放多种Mapping,和其 ...

  4. Python repr() 函数

    Python repr() 函数  Python 内置函数 描述 repr() 函数将对象转化为供解释器读取的形式. 语法 以下是 repr() 方法的语法: repr(object) 参数 obje ...

  5. 兼容谷歌、火狐、IE7.0以上浏览器div+css实现的带有蒙版的半透明弹窗效果[xyytit]

    整个页面变暗的蒙版效果,带有半透明边框的弹窗,用在网站里一定很酷. 最初见与奢饰品购物网站YMALL,后边研究了下,自己做了这个实例. 技术要点:css中几种透明样式的使用.不同的样式在不同的浏览器中 ...

  6. Jmeter发送某个request时而成功,时而失败(处理办法:失败的时候尝试重新发送这个HTTP request)

    Jmeter发送某个request时而成功,时而失败 Maybe it’s Jmeter’s problem, after all, is not a commercial software. And ...

  7. android 网站上下的 adt 不能显示没有安装的

    问题描述 使用SDK Manager更新时出现问题Failed to fetch URL https://dl-ssl.google.com/android/repository/repository ...

  8. Windows Server 2012如何实现双网卡绑定

    在windows server 2012 之前我们在服务器上如果要实现双网卡绑定则需要向服务器厂家所要相应的软件,但是现在强大的windows server 2012的到来使我们省去了所有的麻烦,因为 ...

  9. 2018.07.03 POJ 3348 Cows(凸包)

    Cows Time Limit: 2000MS Memory Limit: 65536K Description Your friend to the south is interested in b ...

  10. 2018.08.18 NOIP模拟 travel(贪心)

    Travel 题目背景 SOURCE:NOIP2015-SHY4 题目描述 小 A 要进行一次旅行.这回他要在序号为 1 到 n 的 n 个城市之间旅行.这 n 个城市之间共有 m 条连接两个城市的单 ...