一、需要的jar包

spring.jar

commons-loggin.jar

commons-loggin.jar

commons-annotation.jar

二、项目结构

三、entity

package com.team.model;

public class User {

	private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
} }

  

四、dao

package com.team.dao;

import com.team.model.User;

public interface UserDAO {
public void print();
}

  

package com.team.dao;

import org.springframework.stereotype.Component;

import com.team.model.User;

@Component("u")
public class UserDAOImpl implements UserDAO{ @Override
public void print() {
System.out.println("auto");
} }

  

五、service

package com.team.service;

import com.team.model.User;

public interface UserService {

	public void add();

}

  

package com.team.service;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import com.team.dao.UserDAO;
import com.team.model.User; @Component("userService")
public class UserServiceImpl implements UserService{ private UserDAO userDAO; public UserDAO getUserDAO() {
return userDAO;
} @Resource(name="u")
public void setUserDAO(UserDAO userDAO) {
this.userDAO = userDAO;
} public void add()
{
userDAO.print(); }
}

  

六、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-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <context:annotation-config/>
<context:component-scan base-package="com.team"></context:component-scan> </beans>

  

七、测试

package com.team.service;

import static org.junit.Assert.*;

import org.junit.Test;
import org.omg.CORBA.portable.ApplicationException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class UserServiceTest { @Test
public void testAdd() {
ApplicationContext ap=new ClassPathXmlApplicationContext("beans.xml");
UserService u=(UserService) ap.getBean("userService");
u.add();
} }

  

八、效果

@Component:类似把bean放进容器中

@Resource、@Autowired:从容器取出对象

spring案列——annotation配置的更多相关文章

  1. spring案列——xml配置

    一.需要的jar包 spring.jar(官网下载) commons-logging.jar 二.项目结构 三.entity(实体类) package com.team.model; public c ...

  2. Spring MVC的配置文件(XML)的几个经典案列

    1.既然是配置文件版的,那配置文件自然是必不可少,且应该会很复杂,那我们就以一个一个的来慢慢分析这些个经典案列吧! 01.实现Controller /* * 控制器 */ public class M ...

  3. 2021年-在windwos下如何用TOMACT发布一个系统(完整配置案列)

    2021年新年第一篇:博主@李宗盛-关于在Windwos下使用TOMCAT发布一个系统的完成配置案列. 之前写过关于TOMCAT的小篇幅文档,比较分散,可以作为对照与参考. 此篇整合在一起,一篇文档写 ...

  4. SpringCloud断路器(Hystrix)和服务降级案列

    断路器(Hystrix) 为什么需要 Hystrix? 在微服务架构中,我们将业务拆分成一个个的服务,服务与服务之间可以相互调用(RPC).为了保证其高可用,单个服务又必须集群部署.由于网络原因或者自 ...

  5. Spring学习资料以及配置环境

    一.Spring4 1.介绍 新特性 SpringIDE 插件 IOC DI 在 Spring 中配置 Bean 自动装配 Bean 之间的关系(依赖.继承) Bean 的作用域 使用外部属性文件 S ...

  6. SpringMvc的xml配置与annotation配置的例子的区别

    1.导入jar包时,要在xml配置基础上加 spring-aop-4.2.2.RELEASE.jar (注解的时候需要) 2.编写controller的时候要annotation需要做相关配置即红色部 ...

  7. Quartz 在 Spring 中如何动态配置时间--转

    原文地址:http://www.iteye.com/topic/399980 在项目中有一个需求,需要灵活配置调度任务时间,并能自由启动或停止调度. 有关调度的实现我就第一就想到了Quartz这个开源 ...

  8. spring amqp rabbitmq fanout配置

    基于spring amqp rabbitmq fanout配置如下: 发布端 <rabbit:connection-factory id="rabbitConnectionFactor ...

  9. spring,mybatis事务管理配置与@Transactional注解使用[转]

    spring,mybatis事务管理配置与@Transactional注解使用[转] spring,mybatis事务管理配置与@Transactional注解使用 概述事务管理对于企业应用来说是至关 ...

随机推荐

  1. Ansiable Manage MySQL global variables

    mysql_variables - Manage MySQL global variables New in version 1.3. Synopsis Requirements (on host t ...

  2. IntelliJ IDEA SVN

    第一步:下载svn的客户端,通俗一点来说就是小乌龟啦!去电脑管理的软件管理里面可以直接下载,方便迅速 下载之后直接安装就好了,但是要注意这里的这个文件也要安装上,默认是不安装的,如果不安装,svn中的 ...

  3. Spring Retry

    最近组内准备将项目中原有的重试功能抽取出来重构为一个重试平台,由于对重试的功能要求比较高,采用了不少中间件和框架(jimdb,jproxy, Elastic-Job ,JMQ,Hbase, Disru ...

  4. appcan更新

    升级用到了config.xml文件中配置的‘更新地址‘所填写的url,此url开发者可任意配置自己的服务器地址* 当app执行uexWidget.checkUpdate()时,AppCan会请求上述u ...

  5. RWIGS and LORBIT (1)

    RWIGS and LORBIT  是两个与局域态密度或投影态密度相关的参数:RWIGS指的是Wigner–Seitz radius,LORBIT前面的LO指的就是Local.         这两个 ...

  6. input限制数字输入

    onkeyup="this.value=this.value.replace(/\D/g,'')"

  7. Plants vs. Zombies(二分好题+思维)

    Plants vs. Zombies http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5819 BaoBao and DreamG ...

  8. 帧动画 连续播放多张图片动画 以及ui动画 SoundPool

    drawable下有很多图片  可以 <?xml version="1.0" encoding="utf-8"?> <animation-li ...

  9. struts框架问题四之获取到值栈的对象

    4. 问题四 : 如何获得值栈对象 * 获得值栈对象 有三种方法 * ValueStack vs1 = (ValueStack) ServletActionContext.getRequest().g ...

  10. jQuery控制TR显示隐藏

    参考链接:http://www.jb51.net/article/51221.htm 通过jQuery的hide和show方法即可.