记录一次spring以构造器方式进行依赖注入的错误

自己的代码如下:(MVC模式)

我的文档结构图:

这是主测试类:

package com.gzz.spring04_DI;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestUserController {

    private UserController userController;

public static void main(String[] args) {

    ApplicationContext applicationContext = new ClassPathXmlApplicationContext("com/gzz/spring04_DI/applicationContext.xml");           

    UserController userController = new UserController();

    userController.login();

}
}

下面是Controller:

package com.gzz.spring04_DI;
public class UserController {

    private UserService userService;

    public void login() {

        userService.login();

    }
    public UserController() {

        super();

    }
    public UserController(UserService userService) {

        super();

        System.out.println("我是UserController带参数的构造方法,我被执行了");

        this.userService = userService;

    }

}


下面是Service:

package com.gzz.spring04_DI;
public class UserService {

    private UserDao userDao;

public void login() {

userDao.login();
}
    public UserService () {}
    public UserService (UserDao userDao) {
        System.out.println("UserService带参数的构造器");
        this.userDao=userDao;
    }
}

下面是Dao:


package com.gzz.spring04_DI;
public class UserDao {
public void login() {
System.out.println("登录成功");
}
}


我的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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-4.3.xsd">        
          
    <bean name="usercontroller" class="com.gzz.spring04_DI.UserController">
        <constructor-arg name="userservice" ref="userservice" ></constructor-arg>
    </bean>
    <bean name="userservice" class="com.gzz.spring04_DI.UserService" >
        <constructor-arg name="userdao" ref="userdao" ></constructor-arg>
    </bean>
    <bean name="userdao" class="com.gzz.spring04_DI.UserDao"></bean>
</beans>

大家知道我错在哪里吗?


我在以构造器方式进行依赖注入时写的name是:

扫描二维码关注公众号,回复: 3499156 查看本文章

但是我声明依赖关系的时候是:

这就相当于spring根据你要声明的要去依赖别的类的那个类竟然在创建的时候找不到。

这个错误等同于在xml配置中配置了一个bean,<bean name=”userController” class=”....”>

。但是在test类中得到spring为你提供的对象userController时你却用了这样的接收语句:

UserController userController = (UserController) applicationContext.getBean("usercontroller");

spring可是大小写敏感的,它可不认识usercontroller,它的内存中可只有叫userController名字的对应包对应类下的对象。

同样,我们可以猜想一下,当spring要给你依赖注入的对象时(以构造方法的形式注入),首先要去你的那个类下找到你的声明好的那个对象,只不过现在是在栈里的一个变量,它的值是null,还没有new,也就是意味着没有挂载任何具体对象的内存,那如果这时候你给spring说我要找到的变量是a,但是spring找找找,没有a,只有最相似的A,spring是大小写敏感的,那么这时候是不是就要(开发spring的人)给spring提供错误的描述好让别人看我的spring更智能,还能给你错误的提示(实则程序的健壮性好)。但我实在是看不懂他给的错误提示,可能现在spring还没有进化到哪个程度(还不够智能)。

 

另外一个与上一个错误比起里无关紧要的错误就是我在test里面创建的是自己的对象。一定要用spring创造好的对象啊,不然spring没办法管理你创建的对象。这就是控制反转IoC。

交给spring去做,然后调调里面的方法搭搭积木。但spring是别人写的,毕竟不是自己的,自己不可能对他那么熟悉。那开发spring的人肯定不会犯这样的错误,毕竟开发spring的思想在哪儿放着呢。

猜你喜欢

转载自blog.csdn.net/u010563350/article/details/82925375