发表于: 2017-09-23 23:49:15

1 812


先说一下为什么拖了两天日报,因为去单位办了离职,准备了一下要去线下。

也不知道自己合不合格。师兄帮忙鉴定一下。

今天完成的事。

【1】学习了一下,依赖注入。

    Spring提供了三种依赖的方式

     1)在XML中进行显式配置。 

     2)在Java中进行显式配置。

     3)隐式的bean发现机制和自动装配。

据说第三种是现在比较流行的,但我发现博客大多采用的都是第一种,我也先对照着第一种搭建一下注入好了。

我先是在UserServiceImpl中添加了私有属性。

private String name;
public void setname(String name){
this.name=name;
}
@Test
public void sayHello(){
System.out.print("hello,测试类"+name );
};

原始方式我想要调用这个usi.sayHello();就需要设置一个name

usi.setname("meimei" );

但是通过bean的依赖注入

<bean id="userService" class="DAO.UserServiceImpl">

<property name="name" value="小张"/>

</bean>


public class CustomerService {
   private CustomerDaoImpl customerDao;

   public void save(){
           System.out.println("我是业务层。。。");
           customerDao.save()
;
public void setCustomerDao(CustomerDaoImpl customerDao) {
   this.customerDao = customerDao;
}
public class CustomerDaoImpl {
   public void save(){
       System.out.println("我是持久层ervice 。。。");
   }
}
<bean id="customerDaoImpl" class="demo2.CustomerDaoImpl"/>
<bean id="customerService" class="demo2.CustomerService">
   <property name="customerDao" ref="customerDaoImpl"/>
</bean>
@Test
public void run2(){
   ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
   CustomerService cs=(CustomerService) ac.getBean("customerService");
    cs.save();
}

遇到的问题


bean标签报红

解决方法

没解决掉,虽然报红,但程序运行正常。

收获

 对Spring的依赖注入有了一些了解

明天要做的事

继续学习Spring



返回列表 返回列表
评论

    分享到