Spring框架文档
https://docs.spring.io/spring-framework/docs/current/spring-framework-reference/
一.IOC实例化对象的三种方式:
<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">
<!--构造函数实例化-->
<!-- 1.调用无参构造函数 -->
<bean id="person1" class="com.imooc.spring.instance.Person"></bean>
<!-- 2.调用有参构造函数 -->
<bean id="person2" class="com.imooc.spring.instance.Person">
<constructor-arg name="name" value="哈哈(构造函数)"/>
<constructor-arg name="age" value="18"/>
</bean>
<!-- 通过静态工厂创建对象-->
<bean id="person3" class="com.imooc.spring.instance.PersonStaticFactory" factory-method="createPerson">
<constructor-arg name="name" value="呵呵(静态工厂)"/>
<constructor-arg name="age" value="20"/>
</bean>
<!-- 通过实例工厂创建对象-->
<bean id="instanceFactory" class="com.imooc.spring.instance.PersonFactory"></bean>
<bean id="person4" factory-bean="instanceFactory" factory-method="createPerson">
<constructor-arg name="name" value="嘻嘻(实例工厂)"/>
<constructor-arg name="age" value="22"/>
</bean>
</beans>
二.DI四种注入方式
1.setter注入
2.构造器注入
3.注解注入
4.JavaConfig
UserService
package com.imooc.spring.injection;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
//@Service("userService")
public class UserService {
// @Resource(name="userDAO")
private UserDAO userDAO;
public UserService() {
System.out.println(this + "已创建");
}
//通过构造函数注入
public UserService(UserDAO userDAO) {
this.userDAO = userDAO;
System.out.println(this + ":正在调用构造函数注入,UserService(" + userDAO + ")");
}
//通过Setter方法注入
public void setUserDAO(UserDAO userDAO) {
this.userDAO = userDAO;
System.out.println(this + ":正在调用Setter方法注入,setUserDAO(" + userDAO + ")");
}
public void createUser(){
System.out.println(this + ":正在调用UserService.createUser()");
userDAO.insert();
}
}
UserDAO
package com.imooc.spring.injection;
public interface UserDAO {
public void insert();
}
UserDAOImpl
package com.imooc.spring.injection;
import org.springframework.stereotype.Repository;
//@Repository("userDAO")
public class UserDAOImpl implements UserDAO{
public UserDAOImpl(){
System.out.println(this + "已创建");
}
public void insert() {
System.out.println(this + ":正在调用UserDAOImpl.insert()");
}
}
AppConfig
package com.imooc.spring.injection;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class AppConfig {
@Bean(name="userDAO")
public UserDAO userDAO(){
return new UserDAOImpl();
}
@Bean(name="userService")
public UserService userService(){
UserService userService = new UserService();
userService.setUserDAO(this.userDAO());
return userService;
}
}
InjectionRunner
package com.imooc.spring.injection;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class InjectionRunner {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:injection.xml");
UserService userService = (UserService)ctx.getBean("userService");
userService.createUser();
}
}
injection.xml
<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.imooc.spring"/>
<!--<bean id="userDAO" class="com.imooc.spring.injection.UserDAOImpl"/>
<bean id="userService" class="com.imooc.spring.injection.UserService">
<!–<property name="userDAO" ref="userDAO"/>–>
<constructor-arg name="userDAO" ref="userDAO"/>
</bean>-->
</beans>
三.自动装配 、spring注解开发、xml与注解
1.自动装配
<bean id="cat" class="Cat"/>
<bean id="dog" class="Dog"/>
<bean id="people" class="People" autowire="byName">
<property name="name" value="red"/>
</bean>
ByName 会自动在容器上下文中查找,和自己对象set方法后面的值对应的beanid
ByType 会自动在容器上下文中查找,和自己对象属性类型相同的bean
注解实现自动装配 <context:annotation-config/> 注解驱动
@Autowired和@Resource的区别
都是用来自动装配的,都可以放在属性字段上
@Autowired 通过byType 的方式实现,而且必须要求这个对象存在
@Resource 默认通过ByName 的方式实现,如果找不到名字,则通过ByType实现,如果俩个都找不到就报错
2.spring注解开发
在spring4之后,要使用注解,必须要保证aop包的引入了
使用注解导入context约束,增加注解的支持
@Nullable 这个字段可以为空了
@Component组件 等价于<bean id="" class=""/> 名字默认类名的小写
@Value 等价于<property name="" value=""/>
衍生的注解 mvc三层架构分层 (@Resposity,@Service,@Controller) 把类放到容器中
@Scope(scopeName="singleton") 作用域
3.xml与注解
xml更加万能,适用于任何场合,维护简单方便
注解 不是自己的类用不了,维护相对复杂
最佳实践
xml用来管理bean
注解 完成属性的注入