依赖注入的方式有3种:----你去注入属性的时候有这3种注入方式;
代码示例:
接口注入
/*
这是一个语言转换的接口
*/
public interface ChangeLetter {
public String change() throws Exception;
}
实现类
/*
这是语言转换接口的一个实现类将字符串转换成小写
*/
public class ChangetoLowerCase implements ChangeLetter{
private String str;
public String getStr() {
return str;
}
public void setStr(String str) {
this.str = str;
}
@Override
public String change() throws Exception {
return str.toLowerCase();
}
}
bean.xml
测试类
public class Test {
public static void main(String[] args) throws Exception {
ApplicationContext applicationContext=new
ClassPathXmlApplicationContext("com/hfc/intface/bean.xml");
ChangeLetter changeLetter=applicationContext.getBean("ChangetoLowerCase",
ChangetoLowerCase.class);
System.out.println( changeLetter.change());
}
}
package com.wdhcr.bean;
public class Student {
private String name;
private int age;
private String address;
public Student() {
}
public Student(String name, int age, String address) {
this.name = name;
this.age = age;
this.address = address;
}
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;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
", address='" + address + '\'' +
'}';
}
}
Person类
package com.wdhcr.bean;
/*
* 这是实体类,有它的相关的成员变量,并生成了它的get set方法 ,
* 还有带参构造器,和重写了它的tostring方法
* */
public class Person {
private String name;
private int age;
private Student student;
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
public Person() {
}
public Person(String name, int age, Student student) {
this.name = name;
this.age = age;
this.student = student;
}
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 "Person{" +
"name='" + name + '\'' +
", age=" + age +
", student=" + student +
'}';
}
}
bean.xml文件依赖注入
张三
13
测试类代码
package com.wdhcr.test;
import com.wdhcr.bean.Person;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/*
* 这是person的测试类
* 使用spring框架创建person的一个对象
* */
public class PersonTest {
public static void main(String[] args) {
ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("bean.xml");
Person person = classPathXmlApplicationContext.getBean("person", Person.class);
System.out.println(person);
System.out.println(person.getName()+"\t"+person.getAge());
System.out.println("-------------");
Person person1 = classPathXmlApplicationContext.getBean("person1", Person.class);
System.out.println(person1);
System.out.println(person1.getName()+"\t"+person1.getAge());
}
}
autowire byName (按名称自动装配)
由于在手动配置xml过程中,常常发生字母缺漏和大小写等错误,而无法对其进行检查,使得开发效率降低。采用自动装配将避免这些错误,并且使配置简单化。
当一个bean节点带有 autowire byName的属性时。
autowire byType (按类型自动装配)
constructor
代码示例
Dog类
package com.wdhcr.auto;
/*
*
*这是dog的实体类使用xml文件注入
* 生成它的set get方法和带参和无参构造
* 重写它的tostring方法
* */
public class Dog {
private String name;
private Integer age;
private String address;
public Dog() {
}
public Dog(String name, Integer age, String address) {
this.name = name;
this.age = age;
this.address = address;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "Dog [name=" + name + ", age=" + age + ", address=" + address + "]";
}
}
package com.wdhcr.auto;
/*
*
*这是cat的实体类使用xml文件注入
* 生成它的set get方法和带参和无参构造
* 重写它的tostring方法
* */
public class Cat {
private String name;
private Integer age;
private String address;
public Cat() {
}
public Cat(String name, Integer age, String address) {
this.name = name;
this.age = age;
this.address = address;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "Cat{" +
"name='" + name + '\'' +
", age=" + age +
", address='" + address + '\'' +
'}';
}
}
package com.wdhcr.auto;
/*
*
*这是user的实体类使用xml文件注入
* 生成它的set get方法和带参和无参构造
* 重写它的tostring方法
* */
public class User {
private String name;
private Integer age;
private String address;
private Dog dog;
private Cat cat;
public User() {
}
public User(String name, Integer age, String address, Dog dog, Cat cat) {
this.name = name;
this.age = age;
this.address = address;
this.dog = dog;
this.cat = cat;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Dog getDog() {
return dog;
}
public void setDog(Dog dog) {
this.dog = dog;
}
public Cat getCat() {
return cat;
}
public void setCat(Cat cat) {
this.cat = cat;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age=" + age +
", address='" + address + '\'' +
", dog=" + dog +
", cat=" + cat +
'}';
}
}
bean.xml文件
package com.wdhcr.auto;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ClassPathXmlApplicationContext classPathXmlApplicationContext =
new ClassPathXmlApplicationContext("com/wdhcr/auto/bean.xml");
User user = classPathXmlApplicationContext.getBean("user", User.class);
System.out.println(user);
}
}