Ioc和Aop使用扩展
DI注入(设置注入/构造注入/p命名注入)
1.构造注入
第一步(类1)
(类二)
第二步:Xml文件
第三步:单测
3.(DI)注解 1.(小案例)
第一步:xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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">
<context:component-scan base-package="cn.zhujiDI"/>
</beans>
第二步:实体类(Student类)
package cn.zhujiDI; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import javax.annotation.Resource; /** * Student */ @Component("student") public class Student { @Value("阳光") private String name; @Value("18") private int age; public Car getCar() { return car2; } public void setCar(Car car) { this.car2 = car2; } @Resource() private Car car2; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "Student{" + "name=‘" + name + ‘\‘‘ + ", age=" + age + ", car=" + car2 + ‘}‘; } }
时间: 2024-10-31 04:16:45