1.创建UserinfoDao类和Test类
package com.jd.userinfo.dao;
import org.springframework.stereotype.Service;
@Service//添加了service注释
public class UserInfoDao {
}
package com.jd.test;
import org.springframework.context.support.ClassPathXmlApplicationContext;import com.jd.userinfo.dao.UserInfoDao;
public class Test {
public static void main(String[] args) {
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("application.xml");
UserInfoDao userInfoDao = applicationContext.getBean(UserInfoDao.class);
System.out.println(userInfoDao);
applicationContext.close();
}
}
2.配置xml文件
在namespaces处勾选上context
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<!--
扫描com.jd包及其子包下所有“类”,并为添加了@Controller@Service@Component@Repository修饰的类创建对象并存入
调用的是默认构造方法
-->
<context:component-scan base-package="com.jd"></context:component-scan>
</beans>
3.执行test中的main方法
结果为
说明添加了service注释的userinfo类被context:component-scan方法创建了对象,并被输出。
值得注意的是,该标签调用的是默认构造方法,如果userinfo类中不含有默认构造方法,则会报错
例如
package com.jd.userinfo.dao;
import org.springframework.stereotype.Service;
@Service
public class UserInfoDao {
public UserInfoDao(int age) {
}//添加了有参构造方法
}
此时执行main方法
4.@Autowired注释
作用:如果@Controller@Service@Component@Repository修饰的类的成员变量有@Autowired修饰,且在ioc容器中有配置 ,则自动给该属性赋值。
修改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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<bean class="java.util.Date"></bean>
<!--
扫描com.jd包及其子包下所有“类”,并为添加了@Controller@Service@Component@Repository修饰的类创建对象并存入
调用的是默认构造方法
当@Controller@Service@Component@Repository修饰的类的成员变量有@Autowired修饰,则自动给该属性赋值(前提是
在ioc容器中有配置)
-->
<context:component-scan base-package="com.jd"></context:component-scan>
</beans>
在userinfo中添加无参构造方法并创建对象(不赋值)
package com.jd.userinfo.dao;
import java.util.Date;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserInfoDao {
@Autowired
private Date date;
public Date t() {
return date;
}
}
改变test类main方法的输出对象
package com.jd.test;
import org.springframework.context.support.ClassPathXmlApplicationContext;import com.jd.userinfo.dao.UserInfoDao;
public class Test {
public static void main(String[] args) {
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("application.xml");
UserInfoDao userInfoDao = applicationContext.getBean(UserInfoDao.class);
System.out.println(userInfoDao.t());
applicationContext.close();
}
}
输出结果为
可以看到对象被自动赋值