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{@Overridepublic 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/beanshttp://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 {@Testpublic void testAdd() {ApplicationContext ap=new ClassPathXmlApplicationContext("beans.xml");UserService u=(UserService) ap.getBean("userService");u.add();}}
八、效果
@Component:类似把bean放进容器中
转载于:https://www.cnblogs.com/dashijie/p/6480935.html
spring案列——annotation配置相关推荐
- MSTP:多生成树实例——应用案列及配置命令(实验)
应用案例: 交换网络采用双核心,核心SW1,核心SW2:拥有VLAN 100个,为了实现二层流量转发的负载均衡,需要在启用MSTP技术或者PVSTP技术,也就是说: ---1-49 VLAN运行一个S ...
- SpringSecurity的简单使用使用案列说明
SpringSecurity Spring Security是 Spring提供的安全认证服务的框架. 使用Spring Security可以帮助我们来简化认证 和授权的过程.官网:https://s ...
- SpringSecurity权限管理框架系列(六)-Spring Security框架自定义配置类详解(二)之authorizeRequests配置详解
1.预置演示环境 这个演示环境继续沿用 SpringSecurit权限管理框架系列(五)-Spring Security框架自定义配置类详解(一)之formLogin配置详解的环境. 2.自定义配置类 ...
- spring事务管理-xml配置aop事务(重点)
刚才咱们是使用了模板操作咱们事务,当然使用模板操作比较low,还得写代码,每个方法都写太费劲了,首先把之前写的先注释掉,把这个transfer这个代码直接复制一份,然后底下展一份,留着注释掉就行了,上 ...
- 具有Spring Boot和Java配置的Spring Batch教程
我一直在努力将Podcastpedia.org的一些批处理作业迁移到Spring Batch. 以前,这些工作是以我自己的方式开发的,我认为现在是时候使用一种更"标准化"的方法了. ...
- thymeleaf案列
点击上方 Java老铁,并选择 设为星标 优质文章和资料会及时送达 1.介绍 Thymeleaf是一个流行的模板引擎,该模板引擎采用Java语言开发,模板引擎是一个技术名词,是跨领域跨平台的概念,在J ...
- spring amqp rabbitmq fanout配置
基于spring amqp rabbitmq fanout配置如下: 发布端 <rabbit:connection-factory id="rabbitConnectionFactor ...
- Quartz 在 Spring 中如何动态配置时间
在项目中有一个需求,需要灵活配置调度任务时间,并能自由启动或停止调度. 有关调度的实现我就第一就想到了Quartz这个开源调度组件,因为很多项目使用过,Spring结合Quartz静态配置调度任务时间 ...
- 面向切面(AOP)之Spring接口方式 schema配置方式 aspectj注解方式
一.初识AOP 关于AOP的学习可以参看帮助文档:spring-3.2.0.M2\docs\reference\html目录下index.html的相关章节 1.AOP:Aspect-O ...
最新文章
- CSS盒模型( CSS margin 属性)
- Windows客户机脱域问题及解决办法
- Intel 公开安全新特性:CET
- python selenium iframe怎么定位_python+selenium四:iframe查看、定位、切换
- 暗棋单机版_中国暗棋游戏下载-中国暗棋下载v1.0.0 安卓版-单机手游网
- 破碎的互联网下,加密技术正在恢复数据主权!
- 设置linux RHEL6.6本地镜像作为yum源
- 在 Mac 上拷贝(复制)、粘贴的方法
- linux开机grub loading,装系统,把grub给装没有了:“grub loading…… grub error 15”
- QPCore Service与NetAssist冲突解决
- 灰太狼大王讲java之抽象类
- 易智联LM401模块学习(一)——初识Lora
- 拼多多token延长时间
- linux网络引流工具,gor 引流快速入门
- jdk8新特性之for循环
- destoon7.0 百度收录自动提交教程
- 产品经理值得读的12本书--苏杰
- python毕业设计选题推荐100例
- Eclipse简单介绍
- android root 卸载app,如何免ROOT卸载安卓系统自带APP
热门文章
- 对中级Linux用户有用的20个命令
- 爱情六十三课,定个开放日
- 7-28 猴子选大王 (20 分) 最易理解的方法
- idea 拉取gitee代码_手把手撸一个 IDEA 插件
- 跨链协议 ChainSwap 将于今晚 9 点在 Uniswap 上进行代币 TOKEN 的 LBP
- TokenInsight:反映区块链行业整体表现的TI指数较昨日同期上涨5.35%
- SAP License:如何导入License
- MySQL高级知识(六)——索引优化
- 1227:Ride to Office
- 【BZOJ1226】[SDOI2009] 学校食堂