1、上上一节我们通过手动配置由程序给属性赋值(我们先来看一下过程)
1.1添加Spring所需的jar包
1.2创建配置文件applicationContext.xml
1.3UserDao接口和UserDaoImpl实现类
package com.fan.dao;
public interface UserDao {
public void show();
}
package com.fan.dao.Impl;
import com.fan.dao.UserDao;
public class UserDaoImpl implements UserDao {
public void show() {
System.out.println("这是UserDao里面的show方法");
}
}
1.4、UserService接口和UserServiceImpl实现类
package com.fan.service;
public interface UserService {
public void show();
}
package com.fan.service.Impl;
import com.fan.dao.UserDao;
import com.fan.service.UserService;
public class UserServiceImpl implements UserService {
private UserDao userDao;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
public void show() {
System.out.println("这是UserService里面的show方法");
userDao.show();
}
}
1.5、UserServlet实现类
package com.fan.servlet;
import com.fan.service.UserService;
public class UserServlet {
private UserService userService;
public void setUserService(UserService userService) {
this.userService = userService;
}
public void show(){
System.out.println("这是UserServlet里面的show方法");
userService.show();
}
}
1.6、在配置文件applicationContext.xml中创建对象
1.7、创建测试类
import com.fan.servlet.UserServlet;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test1_1 {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
UserServlet userServlet =(UserServlet)applicationContext.getBean("servlet");
userServlet.show();
System.out.println(userServlet);
}
}
测试结果如下:
2、通过修改applicationContext.xml中创建对象的配置实现自动注入
注:这里通过调用autowire属性实现自动注入,autowire属性值默认是no,即不自动装配,上面我们改成了byName属性值,这里我们需要注意的是Dao层和Service层的配置对象的id名得和java文件中的属性名一致。
autowire中的属性值:
no:不自动装配(默认值)
byName :属性名=id名 ,调取set方法赋值
byType :属性的类型和id对象的类型相同,当找到多个同类型的对象时报错,调取set方法赋值
constructor :构造方法的参数类型和id对象的类型相同,当没有找到时,报错。调取构造方法赋值
3、注解实现IOC
3.1、在applicationContext.xml配置文件中添加约束
xmlns:context="http://www.springframework.org/schema/context"
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
3.2、在applicationContext.xml配置文件中配置注解扫描:指定扫描包下所有类中的注解,扫描包时,会扫描包的所有子孙包
3.3、加注解
3.3.1添加在类名上
@Component("对象名")
//@Component("对象名") 用来创建对象,如果省略小括号,则当前类名就是对象(首字母变成小写)
@Service("对象名") // service层
@Controller("对象名") // controller层
@Repository("对象名") // dao层
UserDaoImpl实现类加@Component注解
package com.fan.dao.Impl;
import com.fan.dao.UserDao;
import org.springframework.stereotype.Component;
@Component //@Component("对象名") 用来创建对象,如果省略小括号,则当前类名就是对象(首字母变成小写)
public class UserDaoImpl implements UserDao {
public void show() {
System.out.println("这是UserDao里面的show方法");
}
}
UserServiceImpl实现类加@Component注解
package com.fan.service.Impl;
import com.fan.dao.UserDao;
import com.fan.service.UserService;
import org.springframework.stereotype.Component;
@Component //@Component("对象名") 用来创建对象,如果省略小括号,则当前类名就是对象(首字母变成小写)
public class UserServiceImpl implements UserService {
private UserDao userDao;
public UserServiceImpl(UserDao userDao) {
this.userDao = userDao;
}
public void show() {
System.out.println("这是UserService里面的show方法");
userDao.show();
}
}
UserServlet类中加@Component注解
package com.fan.servlet;
import com.fan.service.UserService;
import org.springframework.stereotype.Component;
@Component //@Component("对象名") 用来创建对象,如果省略小括号,则当前类名就是对象(首字母变成小写)
public class UserServlet {
private UserService userService;
public UserServlet(UserService userService) {
this.userService = userService;
}
public void show(){
System.out.println("这是UserServlet里面的show方法");
userService.show();
}
}
测试类
import com.fan.servlet.UserServlet;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test1_1 {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
UserServlet userServlet =(UserServlet)applicationContext.getBean("userServlet");
userServlet.show();
System.out.println(userServlet);
}
}
3.3.2添加在属性上
非对象类型
@Value("属性值")
@Value("属性值")
private String name;
对象类型
@Autowired
默认是按byType注入,如果一个接口类型,同时有两个实现类,则报错,此时可以借助@Qualifier("bean name")
@Qualifier("对象名")
@Qualifier("被注入的对象名")将自动注入中的byType改成byName
@Resource(name="对象名")
说明:@Resource 是java的注释,但是Spring框架支持,@Resource指定注入哪个名称的对象
@Resource(name="对象名") == @Autowired + @Qualifier("name")
UserDaoImpl实现类
package com.fan.dao.Impl;
import com.fan.dao.UserDao;
import org.springframework.stereotype.Component;
@Component //@Component("对象名") 用来创建对象,如果省略小括号,则当前类名就是对象(首字母变成小写)
public class UserDaoImpl implements UserDao {
public void show() {
System.out.println("这是UserDao里面的show方法");
}
}
UserServiceImpl实现类
package com.fan.service.Impl;
import com.fan.dao.UserDao;
import com.fan.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
@Component //@Component("对象名") 用来创建对象,如果省略小括号,则当前类名就是对象(首字母变成小写)
public class UserServiceImpl implements UserService {
@Autowired
@Qualifier("userDaoImpl")
private UserDao userDao;
public void show() {
System.out.println("这是UserService里面的show方法");
userDao.show();
}
}
UserServlet类
package com.fan.servlet;
import com.fan.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
@Component //@Component("对象名") 用来创建对象,如果省略小括号,则当前类名就是对象(首字母变成小写)
public class UserServlet {
@Autowired
@Qualifier("userServiceImpl")
private UserService userService;
public void show(){
System.out.println("这是UserServlet里面的show方法");
userService.show();
}
}
测试类
import com.fan.servlet.UserServlet;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test1_1 {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
UserServlet userServlet =(UserServlet)applicationContext.getBean("userServlet");
userServlet.show();
System.out.println(userServlet);
}
}