一、需要的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. Python递归的经典案例

    目录 : 一.递归的简介 二.递归的经典应用   2.1 递归求阶乘   2.2 递归推斐波那契数列   2.3 二分法找有序列表指定值   2.4 递归解汉诺塔 前言: 当我们碰到诸如需要求阶乘或斐 ...

  2. Dell 服务器安装方法介绍

    大家都知道dell服务器在安装windows系统时都需要有raid卡驱动的加载才可以人道服务器硬盘,下面来介绍一下dell服务器raid卡驱动的加载和系统的安装: 方法一: 使用dell服务器自带的 ...

  3. Ubuntu 分辨率更改 xrandr Failed to get size of gamma for output default

    sudo vim /etc/xorg.conf copy: Section "Monitor" Identifier "Monitor0" VendorName ...

  4. Python bytearray() 函数

    Python bytearray() 函数  Python 内置函数 描述 bytearray() 方法返回一个新字节数组.这个数组里的元素是可变的,并且每个元素的值范围: 0 <= x < ...

  5. 二叉查找树迭代器 · Binary Search Tree Iterator

    [抄题]: 设计实现一个带有下列属性的二叉查找树的迭代器: 元素按照递增的顺序被访问(比如中序遍历) next()和hasNext()的询问操作要求均摊时间复杂度是O(1) 对于下列二叉查找树,使用迭 ...

  6. Thread.currentThread().getName() ,对象实例.getName() 和 this.getName()区别

    原文链接:http://www.cnblogs.com/signheart/p/922dcf75dd5fe6b418d4475af89c4664.html 使用Thread.currentThread ...

  7. YAML教程

    一.简介 YAML是一种人们可以轻松阅读的数据序列化格式,并且它非常适合对动态编程语言中使用的数据类型进行编码.YAML是YAML Ain't Markup Language简写,和GNU(" ...

  8. iOS.AVCaptureSession

    AVCaptureSession的使用容易freeze的问题 1. http://stackoverflow.com/questions/11905505/avcapturesession-stop- ...

  9. [Selenium]验证点了某个Button之后无反应

    期望:点了某个Button,无反应 问题:怎么去验证无反应 WebElement webElement = page.getICORemove(); SeleniumUtil.hover(page.g ...

  10. [BAT]批处理脚本双击可运行,但在定时计划任务中无法执行(当前工作路径不对导致的)

    一开始,红色部分我是用set AutoPath=%cd%,双击可执行,但是将这个批处理脚本放在定时任务中无法执行,后来发现在定时执行的时候,当前工作路径不是批处理脚本所在的路径,而是C:/Window ...