Springset注入(掌握)
Posted 王六六的IT日常
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Springset注入(掌握)相关的知识,希望对你有一定的参考价值。
创建spring配置文件
Alt insert ---->xml 👇
对应包的spring配置文件:
String config = "bao1/applicationContext.xml";
ApplicationContext ctx = new ClassPathXmlApplicationContext(config);
applicationContext.xml
<bean id="mystudent" class="com.bjpowernode.bao1.Student">
</bean>
Student.java
public class Student {
private String name;
private int age;
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 +
'}';
}
}
MyTest.java
@Test
public void test01(){
String config = "bao1/applicationContext.xml";
ApplicationContext ctx = new ClassPathXmlApplicationContext(config);
Student student = (Student) ctx.getBean("mystudent");
System.out.println("Studen:" + student);
}
此时没有给属性赋值:
DI:给属性赋值
简单类型: java中的基本数据类型和String
简单类型的set
注入: spring调用类的set()
方法,通过set()
方法完成属性赋值
语法:
<bean id="xxx" class="yyy">
<property name="属性名" value="简单类型属性值"/>
...
</bean>
applicationContext.xml
<bean id="mystudent" class="com.bjpowernode.bao1.Student">
<property name="name" value="丸子"/> <!--setName("丸子")-->
<property name="age" value="19"/> <!--setAge(19)-->
</bean>
执行test01👇
在set方法中添加输出语句:
public class Student {
private String name;
private int age;
public void setName(String name) {
System.out.println("setName=="+name);
this.name = name;
}
public void setAge(int age) {
System.out.println("setAge=="+age);
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\\'' +
", age=" + age +
'}';
}
}
添加一个无参构造:
public class Student {
private String name;
private int age;
public Student() {
System.out.println("Student的无参数构造方法");
}
public void setName(String name) {
System.out.println("setName=="+name);
this.name = name;
}
public void setAge(int age) {
System.out.println("setAge=="+age);
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\\'' +
", age=" + age +
'}';
}
}
若将set方法去掉则会报错:
set()方法里面是由自己实现的:
添加一个setEmail()方法:
👇
给属性赋值看的是set方法,与属性名无关,与有没有属性无关,看的只是set方法,有set就是正确的
给非自定义类属性赋值
引用类型set注入
语法:
<bean id="xxx" class="yyy">
<property name="属性名" ref="bean的id"/>
...
</bean>
Student.java
public class Student {
private String name;
private int age;
//引用类型
private School school;
public Student() {
System.out.println("Student的无参数构造方法");
}
public void setName(String name) {
System.out.println("setName=="+name);
this.name = "Hello" + name;
}
public void setAge(int age) {
System.out.println("setAge=="+age);
this.age = age;
}
public void setSchool(School school) {
System.out.println("setSchool==" + school);
this.school = school;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\\'' +
", age=" + age +
", school=" + school +
'}';
}
}
School.java
public class School {
private String name;
private String address;
public void setName(String name) {
this.name = name;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "School{" +
"name='" + name + '\\'' +
", address='" + address + '\\'' +
'}';
}
}
用spring体现一下:
School声明在student前后的顺序无所谓(id和ref区分大小写)
<!--引用类型set注入-->
<bean id="mystudent" class="com.bjpowernode.bao2.Student">
<property name="name" value="王六六"/>
<property name="age" value="20"/>
<!--引用类型的赋值-->
<property name="school" ref="mySchool" /><!--setSchool(mySchool)-->
</bean>
<!--声明School-->
<bean id="mySchool" class="com.bjpowernode.bao2.School">
<property name="name" value="北京大学"/>
<property name="address" value="北京的海淀区"/>
</bean>
以上是关于Springset注入(掌握)的主要内容,如果未能解决你的问题,请参考以下文章
以下代码片段是不是容易受到 Rails 5 中 SQL 注入的影响?
初识Spring源码 -- doResolveDependency | findAutowireCandidates | @Order@Priority调用排序 | @Autowired注入(代码片段
初识Spring源码 -- doResolveDependency | findAutowireCandidates | @Order@Priority调用排序 | @Autowired注入(代码片段
安全测试 web安全测试 常规安全漏洞 可能存在SQL和JS注入漏洞场景分析。为什么自己没有找到漏洞,哪么可能存在漏洞场景是?SQL注入漏洞修复 JS注入漏洞修复 漏洞存在场景分析和修复示例(代码片段