Spring框架依赖注入方法示例

网友投稿 269 2023-03-14


Spring框架依赖注入方法示例

在阅读这篇文章之前,大家可以先参阅《理解Spring中的依赖注入和控制反转》一文,了解下依赖注入和控制反转的相关内容。

三种依赖注入的方式

属性注入,通过setter方法注入bean的属性值或依赖的对象 构造注入 工厂方法注入(很少使用)

例子

这里我们使用了spring-4.3.2,maven配置文件

org.springframework

spring-core

${org.springframework-version}

commons-logging

commons-logging

org.springframework

spring-beans

${org.springframework-version}

org.springframework

spring-aop

${org.springframework-version}

org.springframework

spring-context

${org.springframework-version}

commons-logging

commons-logging

1.2

junit

junit

3.8.1

test

applicationContext.xml配置文件

]]>

root

1234

jdbc:mysql://test

com.mysql.jdbc.Driver

1. 下面是简单的属性注入、构造注入的测试类

Car.java

package com.spring.test;

public class Car {

private String name;

private int maxSpeed;

private double price;

public Car() {

}

public Car(String name, double price) {

this.name = name;

this.price = price;

}

public Car(String name, int maxSpeed) {

this.name = name;

this.maxSpeed = maxSpeed;

}

public Car(String name, double price, int maxSpeed) {

this.name = name;

this.price = price;

this.maxSpeed = maxSpeed;

}

public void setPrice(double price) {

this.price = price;

}

@Override

public String toString() {

return "Car [name:" + name + ", price:" + price + ", maxSpeed:" + maxSpeed + "]";

}

}

HelloWorld.java

package com.spring.test;

public class HelloWorld {

private String name;

public HelloWorld() {

System.out.println("HelloWorld constructor...");

}

public String getName() {

return name;

}

public void setName(String name) {

System.out.println("setName:" + name);

this.name = name;

}

@Override

public String toString() {

return "hello," + name;

}

}

Person.java

package com.spring.test;

public class Person {

private String name;

private int age;

private Car car;

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public int getAge() {

return age;

}

public oLcLbhrvoid setAge(int age) {

this.age = age;

}

public Car getCar() {

return car;

}

public void setCar(Car car) {

this.car = car;

}

@Override

public String toString() {

return "Person: [name=" + name + ", age=" + age + ", car=" + car + "]";

}

}

Main.java

package com.spring.test;

import org.junit.Test;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {

public static void main(String[] args) {

HelloWorld hello = new HelloWorld();

hello.setName("barry");

System.out.println("print:"+ hello + "\n");

// 装入 Spring 配置文件

/**

* 装入 Spring 配置文件

* ApplicationContext是IOC容器,它有两个主要实现类(ClassPathXmlApplicationContext和FileSystemXmlApplicationContext)

* ApplicationContext在初始化上下文时就实例化所有单例的Bean

*/

ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

//HelloWorld hello1 = (HelloWorld) context.getBean("helloWorld"); // 通过id获取bean对象

HelloWorld hello1 = context.getBean(HelloWorld.class);

// 通过类型获取bean对象(要求在IOC容器里该类型的对象只能有一个)

System.out.println(hello1);

}

@Test

public void testContructor() {

ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

Car car = (Car) context.getBean("car");

// 通过类型获取bean对象(要求在IOC容器里该类型的对象只能有一个)

Car car1 = (Car) context.getBean("car1");

System.out.println(car);

System.out.println(car1);

Person person = (Person) context.getBean("person");

System.out.println(person);

}

}

2. 下面是集合的测试类

NewPerson.java

package com.spring.test.collections;

import java.util.Map;

import com.spring.test.Car;

public class NewPerson {

private String name;

private int age;

private Map cars;

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 Map getCars() {

return cars;

}

public void setCars(Map cars) {

this.cars = cars;

}

@Override

public String toString() {

return "Person: [name=" + name + ", age=" + age + ", cars=" + cars + "]";

}

}

Person.java

package com.spring.test.collections;

import java.util.List;

import com.spring.test.Car;

public class Person {

private String name;

private int age;

private List cars;

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 List getCars() {

return cars;

}

public void setCars(List cars) {

this.cars = cars;

}

@Override

public String toString() {

return "Person: [name=" + name + ", age=" + age + ", cars=" + cars + "]";

}

}

DataSource.java

package com.spring.test.collections;

import java.util.Properties;

public class DataSource {

private Properties properties;

public Properties getProperties() {

return properties;

}

public void setProperties(Properties properties) {

this.properties = properties;

}

@Override

public String toString() {

return "DataSource: [properties:" + properties + "]";

}

}

Main.java

package com.spring.test.collections;

import org.junit.Test;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {

@Test

public void testCollections() {

ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

Person person = (Person) context.getBean("person3");

System.out.println(person);

NewPerson newPerson = (NewPerson) context.getBean("newPerson");

System.out.println(newPerson);

DataSource dataSource = (DataSource) context.getBean("dataSource");

System.out.println(dataSource);

Person person4 = (Person) context.getBean("person4");

System.out.println(person4);

Person person5 = (Person) context.getBean("person5");

System.out.println(person5);

}

}

总结

以上就是本文关于Spring框架依赖注入方法示例的全部内容,希望对大家有所帮助。如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Java实现发送短信验证码功能
下一篇:api接口文档前端如何接(api接口文档怎么用)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~