您的位置:首页 > 编程语言 > Java开发

######【spring属性注入(Ioc的DI)总结】:注解方式属性注入,属性名任意.=for理解:Aop注入代理对象时,注入被增强类对象时,属性名为proxy(自定义)。

2017-10-10 14:49 1121 查看
 ============================

//===spring 注解方式属性注入,属性名任意。不需要和bean的id相同。
@Service("userService")
public class UserService {

//1.通过注解方式完成属性注入:自动注解,根据类名来注入对象
// @Autowired
// private UserDao userDao;

//2.需指定注入的是哪个对象
@Resource(name="userDao")
//private UserDao userDao;
private UserDao uDao;//===也可以。
public void add(){ System.out.println("add service........"); //userDao.add();
uDao.add(); }}
===便于理解:Aop注入代理对象时,注入被增强类,对象名不一样的。代码如下:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class Demo {
//aop开发配置完成,只能获得到代理对象。===aop就是封装了两种代理。
@Resource(name="userService")//相当于属性注入。注解封装了set方法
private UserService proxy;//老师代码。===名称可以不一样?###【注解方式注入属性,属性名本就可以任意】
//private UserService userService;//功能也实现了。

@Test
public void fun1(){
proxy.save();
//userService.save();
}

@Test
public void fun2(){
proxy.delete();
//userService.delete();
}

}

======【这里只是为了
①理解aop注入代理对象,属性名和注解的bean 的id不同。同时
②熟练两种方式的属性注入 的特点。】
==========================================================
首先是基础:熟练两种方式的属性注入
spring属性注入总结:
######实用步骤:

属性注入:xml方式:两步:bean配置property属性注入 + 声明对象属性和set方法(必须有)。===属性名严格
属性注入:注解方式:两步:实体类添加@Component注解 + 声明对象属性和添加@Autowared注解(不需要set方法)===属性名任意

===熟练这两种方式的两步,自然理解了。
代码:
public class ActionSelf extends ActionSupport {
//【spring属性注入总结】 【xml、注解两种方式】 【xml方式不需要用任何注解!!!】
//@Autowired只负责注解方式 属性注入(DI)。###### xml方式属性注入 不需要注解!
//属性注入:xml方式:两步:bean配置property属性注入 + 声明对象属性和set方法(必须有)。
//属性注入:注解方式:两步:实体类添加@Component注解 + 声明对象属性和添加@Autowared注解(不需要set方法)
//概念:
//IOC:就是创建对象!控制反转:对象交给spring创建。【只负责创建对象】===实现就一个步骤:bean配置或@Component
//DI:就是属性注入!依赖注入。===实现2个步骤:
// 重点【xml方式属性注入容易忘:①xml有property了,属性注入还没完呢!②必须要有对象属性和set方法】
//==######=这种service不是springIOC属性注入。而是:struts数据封装。属性驱动方式?错!
//spring Xml方式属性注入!
private UserService us;//xml方式:这里不需要加@Autowared
public void setUs(UserService us) {
this.us = us;
}
@Test
//注意:不配置事务属性.hibernate事务默认属性是readonly=true
//===报错:InvalidDataAccessApiUsageException:
//Write operations are not allowed in read-only mode (FlushMode.MANUAL):
//Turn your Session into FlushMode.
//是正常的。
//===spring配置完事务,在运行ok。hibernate事务属性一定要设置:readonly=false
public void fun2(){
User u = new User();
u.setUser_name("卢青");
us.save(u);
}
}
========######=====属性注入 总结:【属性注入:要么纯xml,要么纯注解】
案例:
①纯xml方式注入属性:=====【严格:属性名必须和bean id值相同;必须提供set方法】
步骤①
<!--纯xml方式属性注入: 目标对象 -->
<bean id="bookService" class="com.itheima.demo1.BookServiceImpl">
<property name="bookDao" ref="bookDao"></property>
</bean>
<bean id="bookDao" class="com.itheima.demo1.BookDaoImpl"/>
步骤②
service定义对象属性,必须提供set方法。
②纯注解方式注入属性:【属性名任意,不用提供set方法】----但是推荐还是和bean的id值一样。
步骤① 实体加上@Component 。。。4种。
dao:
@Repository//===只等效<bean id="" class="">
public class UserDaoImpl extends HibernateDaoSupport implements UserDao {

@Autowired//===老师代码===@Autowired @Resource 等效:<property>,调set方法注入属性。
public void setSf(SessionFactory sf){
super.setSessionFactory(sf);
}
//beanID默认类名首字母小写。
service:
@Service
//@Transactional(readOnly=false)
public class UserServiceImpl implements UserService {
//@Autowired//==IOC
@Resource(name="userDaoImpl")
private UserDao ud;

反正有种情况会出现:报错:===//NoSuchBeanDefinitionException: No bean named 'bookDao' is defined
===### 发现这种报错原因:两种。
①dao类的bean没有配置。(使用注解就不需要配置所有的bean了。只需要开启注解扫描。)
②xml方式注入属性:属性名和bean的id值不同。不匹配。===【xml方式非常严格】:属性名和bean的id值(或name属性值)必须相同。反射调用set方法。
###我的代码:使用注解方式同时,xml文件还配置了service的bean。复制的代码,有点混乱。
====反映了:两种属性注入并不熟悉,熟练使用程度不够。
-------#### Aop注入代理对象时可以使用自定义属性名proxy。加深理解,别人写的,也要能认识
spring配置只有:
<context:component-scan base-package="com.itheima"></context:component-scan>
service dao 使用注解。属性也用注解。

半xml半注解方式:===偶尔试试的。奇葩。===不使用=====后面以后不要看。。。。。
【只使用一个注入属性注解。xml没有开启注解扫描。配置bean,没有注入】===竟然也可以
①测试:
@RunWith(SpringJUnit4ClassRunner.class)//===spring-test包里。
@ContextConfiguration("classpath:applicationContext.xml")
public class Demo1 {
@Resource(name="bookService")//===name值必须和属性名一样。否则创建Demo3(当前类对象失败)
private BookService bookService;

public void setBookService(BookService bookService) {
this.bookService = bookService;
}

@Test
public void test1(){
bookService.save();
//bookService.update();
}
②service
//@Component(value="bookService")
public class BookServiceImpl implements BookService{

@Resource(name="bookDao")
BookDao bookDao;
//NoSuchBeanDefinitionException: No bean named 'customerDao' is defined
//BookDao dao;
public void setBookDao(BookDao bookDao) {
this.bookDao = bookDao;
}

@Override
public void save(){
//int a = 10/0;//被增强的方法报错,最终通知 仍然执行:用于释放资源等必须执行的操作。
System.out.println("业务 save...");
bookDao.save();
//dao.save();
}
③dao
//@Component(value="bookDao")
public class BookDaoImpl implements BookDao{

@Override
public void save(){
//int a = 10/0;//被增强的方法报错,最终通知 仍然执行:用于释放资源等必须执行的操作。
System.out.println("dao save...");
}

④配置:
<?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"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd"> <!--纯xml方式属性注入: 目标对象 -->
<!-- <bean id="bookService" class="com.itheima.demo1.BookServiceImpl">
<property name="bookDao" ref="bookDao"></property>
</bean>
<bean id="bookDao" class="com.itheima.demo1.BookDaoImpl"/> -->
<bean id="bookService" class="com.itheima.demo1.BookServiceImpl" />
<bean id="bookDao" class="com.itheima.demo1.BookDaoImpl"/>


============================
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐