spring案列——annotation配置
一、需要的jar包
spring.jar
commons-loggin.jar
commons-loggin.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放进容器中
spring案列——annotation配置的更多相关文章
- spring案列——xml配置
一.需要的jar包 spring.jar(官网下载) commons-logging.jar 二.项目结构 三.entity(实体类) package com.team.model; public c ...
- Spring MVC的配置文件(XML)的几个经典案列
1.既然是配置文件版的,那配置文件自然是必不可少,且应该会很复杂,那我们就以一个一个的来慢慢分析这些个经典案列吧! 01.实现Controller /* * 控制器 */ public class M ...
- 2021年-在windwos下如何用TOMACT发布一个系统(完整配置案列)
2021年新年第一篇:博主@李宗盛-关于在Windwos下使用TOMCAT发布一个系统的完成配置案列. 之前写过关于TOMCAT的小篇幅文档,比较分散,可以作为对照与参考. 此篇整合在一起,一篇文档写 ...
- SpringCloud断路器(Hystrix)和服务降级案列
断路器(Hystrix) 为什么需要 Hystrix? 在微服务架构中,我们将业务拆分成一个个的服务,服务与服务之间可以相互调用(RPC).为了保证其高可用,单个服务又必须集群部署.由于网络原因或者自 ...
- Spring学习资料以及配置环境
一.Spring4 1.介绍 新特性 SpringIDE 插件 IOC DI 在 Spring 中配置 Bean 自动装配 Bean 之间的关系(依赖.继承) Bean 的作用域 使用外部属性文件 S ...
- SpringMvc的xml配置与annotation配置的例子的区别
1.导入jar包时,要在xml配置基础上加 spring-aop-4.2.2.RELEASE.jar (注解的时候需要) 2.编写controller的时候要annotation需要做相关配置即红色部 ...
- Quartz 在 Spring 中如何动态配置时间--转
原文地址:http://www.iteye.com/topic/399980 在项目中有一个需求,需要灵活配置调度任务时间,并能自由启动或停止调度. 有关调度的实现我就第一就想到了Quartz这个开源 ...
- spring amqp rabbitmq fanout配置
基于spring amqp rabbitmq fanout配置如下: 发布端 <rabbit:connection-factory id="rabbitConnectionFactor ...
- spring,mybatis事务管理配置与@Transactional注解使用[转]
spring,mybatis事务管理配置与@Transactional注解使用[转] spring,mybatis事务管理配置与@Transactional注解使用 概述事务管理对于企业应用来说是至关 ...
随机推荐
- PIE结对项目编程
一.题目描述 构造程序并测试,分别是: 1.不能触发Fault. 2.触发Fault,但是不能触发Error. 3.触发Error,但是不能产生Failure. 二.结对对象 ...
- EasyUI_tabs和layout布局, 点击链接打开标签, 重复点击选中标签
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...
- 几个小模板:topology, dijkstra, spfa, floyd, kruskal, prim
1.topology: #include <fstream> #include <iostream> #include <algorithm> #include & ...
- docker在centos和Ubuntu的安装
CentOS: http://blog.csdn.net/wuapeng/article/details/51728614 rpm -Uvh http://www.elrepo.org/elrepo- ...
- VS 窗体大小
锁定窗体大小,就是鼠标停在窗口边框的时候,不能拖动来改变它的大小…… 有两种方法: 1.可以把Form的属性 FormborderStyle 后面选择 FixedDialog 2.或者把Form窗体的 ...
- MongoDB使用场景和局限 (转)
MongoDB的使用场景: 1.Web应用程序.文档能表示丰富的数据结构,建模相同数据库所需的集合数量通常会比使用完全正规化关系型数据库的数据表数量要少.动态查询和二级索引能让你轻松的实现SQL开发者 ...
- discuz回贴通知插件实现-插件后台管理配置
1.登出discuz后台,再次设计插件 2.使用变量
- 分析maven的优点
1.依赖管理: 就是对jar包的管理. 2.项目的一键构建: 编译--->测试--->运行--->打包--->安装 运行一个maven工程(web工程)只需要一个命令:toma ...
- 利用委托机制处理.NET中的异常
WinForm代码 private void button1_Click(object sender, EventArgs e) { try { Convert.ToInt32("abcd& ...
- 使用phpExcel导出excel文件
function export($log_list_export) { require "../include/phpexcel/PHPExcel.php"; require &q ...