1.spring-依赖注入-set注入
第一步:新建一个类HelloSpring
注意:依赖注入的属性需要set方法
package com.gyx.HelloSpring;
public class HelloSpring {
//name 值用 spring 依赖注入 IOC
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void Hello(){
System.out.println("Hello"+name);
}
}
第二步:配置applicationContext.xml
注意:用bean标签来配置注入的类 id表示该bean的唯一标识符;class表示注入类的全限定名
bean标签里面的property标签用来配置属性:name表示类中的属性;value表示属性值
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd">
<!-- 由spring new 一个对象 属性也一起注入 -->
<bean id="user" class="com.gyx.HelloSpring.HelloSpring">
<property name="name" value="VN"></property>
</bean>
</beans>
第三步:写一个测试类HelloTest
注意:用 ClassPathXmlApplicationContext("配置文件名") 可以获取相应配置文件中的存储bean的容器; 用context的getBean方法可以获取对象
package com.gyx.HelloSpring;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class HelloTest {
public static void main(String[] args) {
//取出容器
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//容器里取出对象
HelloSpring hs = (HelloSpring) context.getBean("user");
hs.Hello();
}
}
2.注解完成依赖注入
Bean实现类注解
@Component 三层都能使用
@Controller 控制层(action)
@Service 业务逻辑层(service)
@Repository 数据持久层(dao)
自动装配注解:
@Autowired (默认按类型)自动装配
@Qualifier("装配名") (按名字)自动装配
@Resource java自带的注解
注意:在使用 @Qualifier("装配名")时,需要在该注解前加上@Autowired;装配名是Bean实现类注解后面跟的名字。如:@Controller("tAction"),@Service("tservice"),@Repository("tdao")
使用注解之后,就不用再配置文件中进行配置了。只需要使用自动扫描包的标签就行了。
<context:component-scan base-package="com.gyx.action,com.gyx.service,com.gyx.dao"></context:component-scan>
spring-auto.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"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd">
<context:component-scan base-package="com.gyx.action,com.gyx.service,com.gyx.dao"></context:component-scan>
</beans>
3.构造器注入
第一步:新建一个Student类
注意:构造器注入不用set方法,只需要带参构造函数
package com.gyx.HelloSpring1;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class Student {
private String name;
private int age;
public Student(){
}
public Student(String name,int age){
this.name = name;
this.age = age;
}
public void HelloTest(){
System.out.println(name+","+age);
}
}
第二步:新建一个spring.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"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd">
<!-- spring 构造器注入 -->
<bean id="VN" class="com.gyx.HelloSpring1.Student">
<constructor-arg name="name" value="VN"/>
<constructor-arg name="age" value="12"/>
</bean>
</beans>
第三步:新建一个测试类 HelloTest
package com.gyx.HelloSpring1;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.gyx.HelloSpring.HelloSpring;
public class HelloTest {
public static void main(String[] args) {
//取出容器
ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
//容器里取出对象
Student s = (Student) context.getBean("VN");
s.HelloTest();
}
}