小编ann*_*ger的帖子

Spring MVC - Bean不适用于@Autowired

我在使用@Autowired注释将一个在xml文件中定义的数据源bean转换为Spring中的类时遇到了一些麻烦.

有人会知道这里发生了什么吗?似乎*Impl类中的dataSource没有被连线.

道的context.xml

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://localhost:8889/spring" />
    <property name="username" value="user" />
    <property name="password" value="1234" />
</bean>
Run Code Online (Sandbox Code Playgroud)

PersonDaoImpl.java

public class PersonDaoImpl implements PersonDao {

@Autowired
private DataSource dataSource;  
private JdbcTemplate jdbcTemplate = new JdbcTemplate();


public DataSource getDataSource() {
    return dataSource;
}

public void setDataSource(DataSource dataSource) {
    System.out.println("SETTING DATA SOURCE");
    this.dataSource = dataSource;
}

public List<Person> getPersonList() {
    // TODO Auto-generated method stub
    return null;
}

public void savePerson(Person person) {
    // TODO Auto-generated method …
Run Code Online (Sandbox Code Playgroud)

spring annotations datasource spring-mvc autowired

4
推荐指数
1
解决办法
9788
查看次数

标签 统计

annotations ×1

autowired ×1

datasource ×1

spring ×1

spring-mvc ×1