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注解使用 概述事务管理对于企业应用来说是至关 ...
随机推荐
- 吴裕雄 数据挖掘与分析案例实战(4)——python数据处理工具:Pandas
# 导入模块import pandas as pdimport numpy as np # 构造序列gdp1 = pd.Series([2.8,3.01,8.99,8.59,5.18])print(g ...
- Week4-作业1:阅读笔记与思考
我在这三天时间里阅读了<构建之法>的第四章和第十七章,产生了一些疑问和深层次的思考. 第四章 问题1: 书中第68页提到“注释(包括所有源代码)应该只用ASCII字符,不要用中文或其他特殊 ...
- VScode 安装必备
1.运行程序:
- HTML的实际演练1
1.HTML介绍 一个网站的建立都是HTML的,例如大家可以打开F12就可以看到浏览器的一个开发者模式,就可以看到网页的源代码了: 当然这网页他有很多的标签编写组成的,有的显示文字,段落,有的是个超链 ...
- Docker三剑客之 Compose
简介 Docker-Compose 是 Docker 的一种编排服务,是一个用于在 Docker 上定义并运行复杂应用的工具,可以让用户在集群中部署分布式应用. 通过 Docker-Compose 用 ...
- long类型与int类型的转换
在数据库中会出现,numberic转换为int出错 在网站中,提交form的时候出现long转int失败会报错,跌了两次坑,还有ajax提交的时候long转int会报错.
- Xcrysden-2
XCrySDen -- (X-Window) CRYstalline Structures and DENsities Introduction to use. XCrySDen is a cryst ...
- nyoj86-找球号(一) 【set 二分查找 hash】
http://acm.nyist.net/JudgeOnline/problem.php?pid=86 找球号(一) 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 ...
- Codeforces Beta Round #9 (Div. 2 Only)
Codeforces Beta Round #9 (Div. 2 Only) http://codeforces.com/contest/9 A gcd水题 #include<bits/stdc ...
- 在c#中设置Excel格式
生成excel的时候有时候需要设置单元格的一些属性,可以参考一下: range.NumberFormatLocal = "@"; //设置单元格格式为文本 ange.get_Ran ...