spring-IOC容器
文章目录
- 一 IOC容器
- 1.1 Bean管理-XML方式
- 创建对象
- 注入属性
- set注入属性
- 有参构造注入属性
- p名称空间注入属性
- 注入特殊类型属性
- 注入内部Bean
- 级联赋值
- 第一种
- 第二种
- 1.2 Bean管理-注解方式
- 创建对象
- 注入属性
- @Autowired 根据属性进行注入
- @Qualifier 根据名称进行注入
- 完全注解开发
- 创建配置类:
- 编写测试类
一 IOC容器
1.1 Bean管理-XML方式
创建对象
注入属性
set注入属性
有参构造注入属性
p名称空间注入属性
注入特殊类型属性
注入内部Bean
Dept.java
package com.jh.spring5.bean;
//部门类
public class Dept {
//部门名称
private String dname;
public void setDname(String dname) {
this.dname = dname;
}
@Override
public String toString() {
return "Dept{" +
"dname='" + dname + '\'' +
'}';
}
}
Emp.java
package com.jh.spring5.bean;
//员工类
public class Emp {
//员工名字
private String ename;
//员工性别
private String gender;
//员工所属部门
private Dept dept;
public void setEname(String ename) {
this.ename = ename;
}
public void setGender(String gender) {
this.gender = gender;
}
public void setDept(Dept dept) {
this.dept = dept;
}
public void add(){
System.out.println(ename+","+gender+","+dept);
}
}
bean1.xml
<bean id="emp" class="com.jh.spring5.bean.emp">
<property name="ename" value="lucy"></property>
<property name="gender" value="女"></property>
<property name="dept">
<bean id="deot" class="com.jh.spring5.bean.Dept">
<property name="dname" value="安保部"></property>
</bean>
</property>
</bean>
测试
@Test
public void testbean(){
ApplicationContext context =
new ClassPathXmlApplicationContext("bean1.xml");
Emp emp = context.getBean("emp",Emp.class);
emp.add();
}
级联赋值
第一种
测试
<bean id="emp" class="com.jh.spring5.bean.Emp">
<property name="ename" value="lucy"></property>
<property name="gender" value="女"></property>
<property name="dept" ref="dept"></property>
</bean>
<bean name="dept" class="com.jh.spring5.bean.Dept">
<property name="dname" value="保安lll部"></property>
</bean>
第二种
Emp.java
public Dept getDept() {
return dept;
}
测试
<bean id="emp" class="com.jh.spring5.bean.Emp">
<property name="ename" value="lucy"></property>
<property name="gender" value="女"></property>
<property name="dept" ref="dept"></property>
<property name="dept.dname" value="技pppp术部"></property>
</bean>
<bean name="dept" class="com.jh.spring5.bean.Dept">
<property name="dname" value="保安lll部"></property>
</bean>
1.2 Bean管理-注解方式
创建对象
package com.jh.spring5.service;
import com.jh.spring5.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserService {
public void add(){
System.out.println("service add.......");
}
}
<?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.xsd">
<context:component-scan base-package="com.jh">
</context:component-scan>
</beans>
@Test
public void test(){
ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
UserService userService = context.getBean("userService",UserService.class);
System.out.println(userService);
userService.add();
}
注入属性
列子:在service里注入dao对象
@Autowired 根据属性进行注入
新建 UserDaoImpl .java
package com.jh.spring5.dao;
import org.springframework.stereotype.Repository;
@Repository
public class UserDaoImpl implements UserDao {
@Override
public void add() {
System.out.println("userdaoimpl.....");
}
}
UserService.java里加上
@Service
public class UserService {
@Autowired
private UserDao userDao;
public void add(){
userDao.add();
System.out.println("service add.......");
}
}
@Qualifier 根据名称进行注入
原因:一个接口可能会有多个实现类
1.新建 userDaoImpl2.java
@Repository
public class UserDaoImpl2 implements UserDao {
@Override
public void add() {
System.out.println("userdaoimpl2.....");
}
}
2.UserService.java
@Qualifier(value = “userDaoImpl”)注入userDaoImpl.java里的属性
@Qualifier(value = “userDaoImpl2”)注入userDaoImpl2.java里的属性
@Service
public class UserService {
@Autowired
@Qualifier(value = "userDaoImpl2")
private UserDao userDao;
public void add(){
userDao.add();
System.out.println("service add.......");
}
}
完全注解开发
创建配置类:
代替xml文件
@Configurable
开启组件扫描
@ComponentScan(basePackages = {"com.jh.spring5"})
@Configurable
@ComponentScan(basePackages = {
"com.jh.spring5"})
public class SpringConfig {
}
编写测试类
ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
public class StuTest {
@Test
public void test(){
ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
UserService userService = context.getBean("userService",UserService.class);
System.out.println(userService);
userService.add();
}
}
还没有评论,来说两句吧...