目录
1.构造函数方式注入
这里是主函数
这里是applicationContext.xml主配置文件
真正的Dao层的实现代码UserServiceImpl实现类
2.(1)set方法注入(常用)applicationContext.xml(注入一般类型,引用类型)
UserServiceImpl3实现类
主函数调用
2.(2)set方法注入(常用)applicationContext.xml(注入集合类型,大致可以记为list集合和Map集合)
main方法
UserServiceImpl4实现类
Spring注入的作用是为了解决实现接口类当中可以调用其他类当中的对象以及方法,并且不是硬编码的方式,而是通过获取核心容器对象方式创建新的对象。例如下面我可以通过UserServiceimpl调用dao对象。
1.构造函数方式注入
这里是主函数
public static void main(String[] args) {
// 1. 获取核心容器对象
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
// 1.(1)构造方法注入,传递一个dao对象
IUserService service = context.getBean("userService",IUserService.class);
}
这里是applicationContext.xml主配置文件
<!-- 1.(1)构造方法注入,传递一个dao对象 -->
<bean id="userService"
class="com.bookmanagesystem.service.impl.UserServiceImpl">
<constructor-arg name="dao" ref="userDao"></constructor-arg>
<!-- 实际上是等同于创建了一个new UserServiceImpl(dao) -->
</bean>
<bean id="userDao"
class="com.bookmanagesystem.dao.impl.UserDaoImpl">
</bean>
真正的Dao层的实现代码UserServiceImpl实现类
// 真正的操作由Dao层执行
private IUserDao dao ;
public UserServiceImpl(IUserDao dao) {
super();
//System.out.println(dao.queryUser());
this.dao = dao;
}
public IUserDao getDao() {
return dao;
}
public void setDao(IUserDao dao) {
this.dao = dao;
}
2.(1)set方法注入(常用)applicationContext.xml(注入一般类型,引用类型)
<!-- 1.(2)set方法注入,传递多个参数 -->
<bean id="userService3"
class="com.bookmanagesystem.service.impl.UserServiceImpl3">
<property name="name" value="方国城"></property>
<property name="age" value="99"></property>
<property name="birth" ref="birthday2"></property>
</bean>
<bean id="birthday2" class="java.util.Date"></bean>
UserServiceImpl3实现类
public class UserServiceImpl3 implements IUserService{
private String name;
private int age;
private Date birth;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Date getBirth() {
return birth;
}
public void setBirth(Date birth) {
this.birth = birth;
}
@Override
public String toString() {
return "UserServiceImpl3 [name=" + name + ", age=" + age + ", birth=" + birth + "]";
}
@Override
public String queryUser() {
// TODO Auto-generated method stub
return "";
}
}
主函数调用
public static void main(String[] args) {
// 1. 获取核心容器对象
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
// 2. 根据id获取bean对象的两种方法
//2.set方法注入,传递多个参数
IUserService service3 = (IUserService) context.getBean("userService3");
System.out.println(service3);
// 3.关闭容器(如果不记得关闭容器,最典型的问题就是数据库连接不能释放)
((ClassPathXmlApplicationContext) context).close();
}
2.(2)set方法注入(常用)applicationContext.xml(注入集合类型,大致可以记为list集合和Map集合)
<!-- 2.(1)set方法注入,传递集合 list map -->
<bean id="userService4"
class="com.bookmanagesystem.service.impl.UserServiceImpl4">
<property name="strings">
<array>
<value> zhangsan</value>
<value> lisi</value>
<value> wangwu</value>
</array>
</property>
<property name="list">
<list>
<value> zhangsan</value>
<value> lisi</value>
<value> 方国城</value>
</list>
</property>
<property name="set">
<set>
<value> zhangsan</value>
<value> lisi</value>
<value> 方国城</value>
</set>
</property>
<property name="map">
<map>
<entry key="string" value="shabi"></entry>
<entry key="string1" value="shabi1"></entry>
<entry key="string2" value="shabi2"></entry>
</map>
</property>
<property name="properties">
<props>
<prop key="driver">sajfaskasjn </prop>
<prop key="url">https:://www.baidu.conima </prop>
<prop key="username">root </prop>
<prop key="password">root </prop>
</props>
</property>
</bean>
main方法
public static void main(String[] args) {
// 1. 获取核心容器对象
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
// 2. 根据id获取bean对象的两种方法
// 1.(1)构造方法注入,传递一个dao对象
IUserService service = (IUserService) context.getBean("userService");
//2.set方法注入,传递集合
IUserService service4 = (IUserService) context.getBean("userService4");
service4.queryUser();
// 3.关闭容器(如果不记得关闭容器,最典型的问题就是数据库连接不能释放)
((ClassPathXmlApplicationContext) context).close();
}
UserServiceImpl4实现类
public class UserServiceImpl4 implements IUserService{
private String[] strings;
private List<String> list;
private Set<String> set;
private Map< String, String> map;
private Properties properties;
public void setStrings(String[] strings) {
this.strings = strings;
}
public void setList(List<String> list) {
this.list = list;
}
public void setSet(Set<String> set) {
this.set = set;
}
public void setMap(Map<String, String> map) {
this.map = map;
}
public void setProperties(Properties properties) {
this.properties = properties;
}
public String[] getStrings() {
return strings;
}
public List<String> getList() {
return list;
}
public Set<String> getSet() {
return set;
}
public Map<String, String> getMap() {
return map;
}
public Properties getProperties() {
return properties;
}
@Override
public String toString() {
return "UserServiceImpl4 [strings=" + Arrays.toString(strings) + ", list=" + list + ", set=" + set + ", map="
+ map + ", properties=" + properties + "]";
}
@Override
public String queryUser() {
System.out.println(Arrays.toString(strings));
System.out.println(list);
System.out.println(set);
System.out.println(map);
System.out.println(properties);
return "";
}
}