Spring的核心 依赖注入 DI 切面编程 AOP
spring要引入的jar包有:
commons-logging-1.2
spring-beans-4.1.4
spring-context-4.1.4
spring-core-4.1.4
spring-expression-4.1.4
Spring依赖注入的原理是:Spring是个bean容器,从XML里面读配置,通过反射创建对象,装载到spring容器 (XML加反射)
spring根据bean构造器拿到对象属性
先创个实体类(符合javabean规范)
public class User {
private int id;
private String name;
public User(){
}
public User(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
在src目录右键XMLConfigurationFile创建个spring Config
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="user" class="com.dengweiquan.entity.User">
<!--Spring用运行时会通过这个配置文件,加载这个bean,用反射创建这个bean,我们就不用创建对象就可以用到这个属性了-->
<constructor-arg type="int" value="12345"></constructor-arg>
<constructor-arg type="java.lang.String" value="laowang"></constructor-arg>
</bean>
</beans>
在主函数中写:
public class Main {
public static void main(String[] args) {
//拿到spring上下文 就拿到spring的bean了
ApplicationContext context=new ClassPathXmlApplicationContext("./com/dengweiquan/resource/application-context.xml");
User user=(User) context.getBean("user");//从spring的容器里拿对象 这样就好用多了 提高代码重用性,就避免了用对象时就要new一个对象
System.out.println(user.getId());//输出12345
System.out.println(user.getName()); //输出laowang
System.out.println(user.getClass()); //输出class com.dengweiquan.entity.User
}
}
倘若不通过构造器获取对象属性,在application-context.xml里改成用property取对象属性
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="user" class="com.dengweiquan.entity.User">
<!--Spring用运行时会通过这个配置文件,加载这个bean,用反射创建这个bean,我们就不用创建对象就可以用到这个属性了-->
<!--<constructor-arg type="int" value="12345"></constructor-arg>-->
<!--<constructor-arg type="java.lang.String" value="laowang"></constructor-arg> -->
<property name="id" value="12345"/>
<property name="name" value="laowang" />
</bean>
</beans>
在主函数输出的结果也是一样的
spring根据bean拿到复杂对象属性
application-context.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="user" class="com.dengweiquan.entity.User">
<!--Spring用运行时会通过这个配置文件,加载这个bean,用反射创建这个bean,我们就不用创建对象就可以用到这个属性了-->
<!--<constructor-arg type="int" value="12345"></constructor-arg>-->
<!--<constructor-arg type="java.lang.String" value="laowang"></constructor-arg> -->
<property name="id" value="12345"/>
<property name="name" value="laowang" />
</bean>
<bean id="fatherUser" class="com.dengweiquan.entity.UserFather">
<property name="fatherName" value="laodie"/>
<!--ref里面user的意思是 这个bean参考user的bean-->
<property name="user" ref="user"/>
</bean>
</beans>
private String fatherName;
private User user;
return fatherName;
}
this.fatherName = fatherName;
}
return user;
}
this.user = user;
}
}
//拿到spring上下文 就拿到spring的bean了
ApplicationContext context=new ClassPathXmlApplicationContext("./com/dengweiquan/resource/application-context.xml");
UserFather userFather=(UserFather) context.getBean(UserFather.class);
System.out.println(userFather.getFatherName());//输出laodie
加了@Service注解 经过了包扫描都加入了spring容器中
除了以上那些包 ,还要再引入
aopliance-1.0
spring-aop
aspectjweaver
@Service
public interface UserService {
public void say();
@Service()
public class UserServiceImpl implements UserService {
@Override
public void say() {
}
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"
xmlns:context="http://www.springframework.org/schema/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"
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"
xmlns:context="http://www.springframework.org/schema/context"
>
<!--Spring用运行时会通过这个配置文件,加载这个bean,用反射创建这个bean,我们就不用创建对象就可以用到这个属性了-->
<!--<constructor-arg type="int" value="12345"></constructor-arg>-->
<!--<constructor-arg type="java.lang.String" value="laowang"></constructor-arg> -->
<property name="id" value="12345"/>
<property name="name" value="laowang" />
</bean>
<property name="fatherName" value="laodie"/>
<!--ref里面user的意思是 这个bean参考user的bean-->
<property name="user" ref="user"/>
</bean>
</beans>
public class UserController {
private UserService userService;
return userService;
}
this.userService = userService;
}
}
主函数:
public class Main {
//拿到spring上下文 就拿到spring的bean了
ApplicationContext context=new ClassPathXmlApplicationContext("./com/dengweiquan/resource/application-context.xml");
//spring注解方式注入 MVC model view controller
// @Service 业务逻辑
//@Component 公共组件
//@Controller 控制用户请求 springMVC
UserController userController=context.getBean(UserController.class);
UserService service= userController.getUserService();
service.say(); //输出成功运行
}
}