Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在spring中自动关联属性_Java_Spring_Annotations_Autowired - Fatal编程技术网

Java 如何在spring中自动关联属性

Java 如何在spring中自动关联属性,java,spring,annotations,autowired,Java,Spring,Annotations,Autowired,我试图通过在bean配置中添加一个属性将字段变量自动关联到类中。这样,当spring得到属性中的值时,就可以实例化字符串 我的spring配置文件如下所示 <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schem

我试图通过在bean配置中添加一个属性将字段变量自动关联到类中。这样,当spring得到属性中的值时,就可以实例化字符串

我的spring配置文件如下所示

<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-2.5.xsd">

<bean id="authentication" class="com.quicut.model.authenticationModel">
    <property name="dbURL" value="jdbc:mysql://127.0.0.1:3306/quicut" />
    <property name="userN" value="user1" />
    <property name="userP" value="password" />
    <property name="JDBC_DRIVER" value="com.mysql.jdbc.Driver" />
</bean>
需要依赖关系的类如下所示

public class login extends HttpServlet {
@Autowired
@Qualifier(value="authentication")
authenticationModel aModel;

我对spring很陌生,所以我不知道我做错了什么或遗漏了什么。

作为建议,您可以使用大写字母来命名类,而不是authenticationModel将其命名为authenticationModel

现在,如果您想在特定Java类中自动连接Springbean,那么需要将该类标记为@Controller或@servicebean类型。只有SpringBean可以自动关联其他Bean

在您的例子中,您的登录类没有标记为特定的Springbean,因此您可能需要使用前面的注释之一

例如:

@Service
public class LoginService {
    @Autowired
    @Qualifier(value="authentication")
    AuthenticationModel aModel;
}

1.您没有遵守Java命名约定;2.您不认为错误消息和堆栈跟踪有助于了解错误是什么。我的猜测是您得到了一个NullPointerException,因为servlet不是Spring组件,因此Spring无法自动连接它们的属性。使用SpringMVC。不要使用Spring2.5,它已经过时多年了。
@Service
public class LoginService {
    @Autowired
    @Qualifier(value="authentication")
    AuthenticationModel aModel;
}