Spring实战之@Autowire注解用法详解
作者:cakincqm 时间:2021-11-17 20:37:19
本文实例讲述了Spring实战之@Autowire注解用法。分享给大家供大家参考,具体如下:
一 配置
<?xml version="1.0" encoding="GBK"?>
<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-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<context:component-scan
base-package="org.crazyit.app.service,org.crazyit.app.dao"/>
</beans>
二 dao接口
BaseDao
package org.crazyit.app.dao;
public interface BaseDao<T>
{
void save(T e);
}
ItemDao
package org.crazyit.app.dao;
import org.crazyit.app.domain.*;
public interface ItemDao extends BaseDao<Item>
{
}
UserDao
package org.crazyit.app.dao;
import org.crazyit.app.domain.*;
public interface UserDao extends BaseDao<User>
{
}
三 dao实现类
BaseDaoImpl
package org.crazyit.app.dao.impl;
import org.crazyit.app.dao.*;
public class BaseDaoImpl<T> implements BaseDao<T>
{
public void save(T e)
{
System.out.println("程序保存对象:" + e);
}
}
ItemDaoImpl
package org.crazyit.app.dao.impl;
import org.springframework.stereotype.*;
import org.crazyit.app.dao.*;
import org.crazyit.app.domain.*;
@Component("itemDao")
public class ItemDaoImpl extends BaseDaoImpl<Item>
implements ItemDao
{
}
UserDaoImpl
package org.crazyit.app.dao.impl;
import org.springframework.stereotype.*;
import org.crazyit.app.dao.*;
import org.crazyit.app.domain.*;
@Component("userDao")
public class UserDaoImpl extends BaseDaoImpl<User>
implements UserDao
{
}
四 Bean
Item
package org.crazyit.app.domain;
public class Item
{
}
User
package org.crazyit.app.domain;
public class User
{
}
五 service接口
BaseService
package org.crazyit.app.service;
import org.springframework.stereotype.*;
import org.springframework.beans.factory.annotation.*;
import org.crazyit.app.service.*;
public interface BaseService<T>
{
void addEntity(T entity);
}
ItemService
package org.crazyit.app.service;
import org.springframework.stereotype.*;
import org.springframework.beans.factory.annotation.*;
import org.crazyit.app.service.*;
import org.crazyit.app.domain.*;
@Component
public interface ItemService extends BaseService<Item>
{
}
UserService
package org.crazyit.app.service;
import org.springframework.stereotype.*;
import org.springframework.beans.factory.annotation.*;
import org.crazyit.app.service.*;
import org.crazyit.app.domain.*;
@Component
public interface UserService extends BaseService<User>
{
}
六 Service实现类
BaseServiceImpl
package org.crazyit.app.service.impl;
import org.springframework.stereotype.*;
import org.springframework.beans.factory.annotation.*;
import org.crazyit.app.dao.*;
import org.crazyit.app.service.*;
public class BaseServiceImpl<T> implements BaseService<T>
{
@Autowired
private BaseDao<T> dao;
public void addEntity(T entity)
{
System.out.println("调用" + dao
+ "保存实体:" + entity);
}
}
ItemServiceImpl
package org.crazyit.app.service.impl;
import org.springframework.stereotype.*;
import org.springframework.beans.factory.annotation.*;
import org.crazyit.app.service.*;
import org.crazyit.app.domain.*;
@Component("itemService")
public class ItemServiceImpl extends BaseServiceImpl<Item>
implements ItemService
{
}
UserServiceImpl
package org.crazyit.app.service.impl;
import org.springframework.stereotype.*;
import org.springframework.beans.factory.annotation.*;
import org.crazyit.app.service.*;
import org.crazyit.app.domain.*;
@Component("userService")
public class UserServiceImpl extends BaseServiceImpl<User>
implements UserService
{
}
七 测试类
package lee;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.crazyit.app.service.*;
import org.crazyit.app.domain.*;
public class BeanTest
{
public static void main(String[] args)throws Exception
{
// 创建Spring容器
ApplicationContext ctx = new
ClassPathXmlApplicationContext("beans.xml");
UserService us = ctx.getBean("userService", UserService.class);
us.addEntity(new User());
ItemService is = ctx.getBean("itemService", ItemService.class);
is.addEntity(new Item());
}
}
八 测试
调用org.crazyit.app.dao.impl.UserDaoImpl@b7dd107保存实体:org.crazyit.app.domain.User@42eca56e调用org.crazyit.app.dao.impl.ItemDaoImpl@52f759d7保存实体:org.crazyit.app.domain.Item@7cbd213e
希望本文所述对大家java程序设计有所帮助。
来源:https://blog.csdn.net/chengqiuming/article/details/101172166
标签:Spring,@Autowire注解
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Spring Boot Debug调试过程图解
2023-12-13 10:27:33
![](https://img.aspxhome.com/file/2023/7/62497_0s.png)
JAVA面试题 简谈你对synchronized关键字的理解
2022-09-17 17:06:05
C#中的委托、事件学习笔记
2023-01-21 18:03:49
Spring注解@Scope原理及用法解析
2023-12-06 14:08:17
c#字符串编码编码(encoding)使用方法示例
2022-10-04 07:24:58
JetCache 缓存框架的使用及源码解析(推荐)
2021-07-23 12:21:25
![](https://img.aspxhome.com/file/2023/2/64402_0s.png)
不可不知道的10个java谎言
2022-01-21 10:25:40
springboot返回值转成JSONString的处理方式
2022-09-27 12:04:46
Java分布式事务管理框架之Seata
2023-09-28 11:50:36
![](https://img.aspxhome.com/file/2023/1/63731_0s.png)
springcloud中Ribbon和RestTemplate实现服务调用与负载均衡
2022-06-30 14:58:45
![](https://img.aspxhome.com/file/2023/5/61585_0s.png)
微信小程序获取手机号,后端JAVA解密流程代码
2023-11-29 07:57:26
java通过ip获取客户端Mac地址的小例子
2021-12-22 06:37:07
sin(x)如何求解的java代码实现方法
2022-04-17 16:22:07
![](https://img.aspxhome.com/file/2023/3/64793_0s.jpg)
代码详解Java猴子选王问题(约瑟夫环)
2023-09-16 07:33:43
详解Springboot分布式限流实践
2021-07-12 14:29:18
![](https://img.aspxhome.com/file/2023/7/64217_0s.png)
spring是如何解析xml配置文件中的占位符
2023-12-02 05:57:12
java中的this引用及对象构造初始化
2023-03-07 09:38:17
![](https://img.aspxhome.com/file/2023/6/60996_0s.png)
Java删除二叉搜索树的任意元素的方法详解
2021-10-04 12:27:26
![](https://img.aspxhome.com/file/2023/1/61021_0s.png)
struts中动态方法调用使用通配符
2023-02-01 02:52:43
![](https://img.aspxhome.com/file/2023/6/70496_0s.png)
Java实现获取指定个数的不同随机数
2023-11-14 21:42:34