Spring of ssm framework: how to configure and create objects in xml

little pig 2022-09-23 08:50:49 阅读数:686

springssmframeworkconfigurecreate

This article will briefly talk about itspring中Bean管理和操作.

首先说一下springThere are two ways to manage operations:

  • 基于xml配置文件方式,This is the method used in the first experience.
  • 基于注解方式实现

Let's talk nowxmlDifferent configuration methods of the configured files have different effects.

Actually it has been saidbean,简单的说就是java对象.

Because configuration files are often used later,格式:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" 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">
<!-- Intermediate configuration information -->
</beans>

创建对象和set注入属性

创建对象

无参创建对象

First, create an object with no parameters,这个很简单.

This time it's easy to create a class:

package com.xzd.test;
public class Person {

public void play(){

System.out.println("I like sports fairy,So I played frisbee");
}
}

然后下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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 创建一个person对象 -->
<bean id="person" class="com.xzd.test.Person"></bean>
</beans>

然后体验一下:

public class test {

public static void main(String[] args) {

ApplicationContext applicationContext= new ClassPathXmlApplicationContext("personbean.xml");
Person person1= (Person) applicationContext.getBean("person");// A cast will be added here or as follows
Person person2= applicationContext.getBean("person",Person.class);// This can be determined by parameters herespringWhat type of object is created
person1.play();
person2.play();
}
}

在这里插入图片描述

补充XML标签解释:

  • bean标签,It is the label that needs to be used to create an object.But it can be seen that the corresponding attributes can be added to the tag.
  • bean属性:
    • id : this unique identifier,可以看出id=“person” 然后test类中applicationContext.getBean(“person”).Usually it will be created with the same name as the object,After all, it is easy to identify.
    • class: This property actually indicates that this is to be createdidrepresents that class,The exact location of this class,其实是class=“com.xzd.test.Person”,
    • name: 当然还有一个name属性,But its meaning isid的差不多,所以直接使用id即可.

But this created object,It can be seen that the created object has no attributes or is constructed with no parameters.

Create object with parameters

演示一下:

package com.xzd.test;
public class Person {

String personname;
int age;
public Person(String personname, int age) {

this.personname = personname;
this.age = age;
}
@Override
public String toString() {

return "Person={\"name\":"+this.personname+",\"age\":"+this.age+"}";
}
}

然后配置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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 创建一个person对象 -->
<bean id="person" class="com.xzd.test.Person">
<constructor-arg name="personname" value="我是张三啊"></constructor-arg>
<constructor-arg name="age" value="18"></constructor-arg>
</bean>
</beans>

然后调用:

public class test {

public static void main(String[] args) {

ApplicationContext applicationContext= new ClassPathXmlApplicationContext("personbean.xml");
Person person= applicationContext.getBean("person",Person.class);// This can be determined by parameters herespringWhat type of object is created
System.out.println(person.toString());
}
}

在这里插入图片描述

Explanation Supplement:

  • constructor-arg : Configured with meaningful parameterized construction parameters.当然可以通过name和valueSet parameters and values.

set注入属性

对于属性,When creating the object earlier,Properties via construction parameters,赋值,But the same can be done in the frontsetAttribute assignment is not straightforward:

public class Person {

String personname;
int age;
public void setAge(int age) {

this.age = age;
}
public void setPersonname(String personname) {

this.personname = personname;
}
@Override
public String toString() {

return "Person={\"name\":"+this.personname+",\"age\":"+this.age+"}";
}
}

但是既然聊spring,Naturally, it cannot be used after creating an objectsetPersonname,现在演示:

 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" 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">
<!-- 创建一个person对象 -->
<bean id="person" class="com.xzd.test.Person">
<property name="personname">
<value>我是张三啊</value>
</property>
<property name="age" value="18"></property>
</bean>
</beans>

然后试一下:

public class test {

public static void main(String[] args) {

ApplicationContext applicationContext= new ClassPathXmlApplicationContext("personbean.xml");
Person person= applicationContext.getBean("person",Person.class);// This can be determined by parameters herespringWhat type of object is created
System.out.println(person.toString());
}
}

在这里插入图片描述

标签补充:

  • property : This is the configuration of properties,But this configuration has a premise,That is what needs to be remembered in the objectsetter方法,不然ide会提示.

  • property的属性(Simply list two,In fact, there are many more to be added later):

    • name : This need is the same as for object properties.
    • value :This is for assigning a property,This way there is no need to create an object and then callsetPersonname了.Of course, this can also be used separately as a label

补充p标签

对于set的注入属性,Could be even simpler,进行配置,Now just need to add the configuration filep标签即可:

<?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:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 创建一个person对象 -->
<bean id="person" class="com.xzd.test.Person" p:personname="我是张三啊" p:age="18">
</bean>
</beans>

在这里插入图片描述

Then take a look at this configuration, which can also be annotated.

Supplementary injection of special symbols

Sometimes when creating objects,It will be a special symbol when assigning properties,当然springIt is also possible to inject properties as special symbols.

  • How to inject null values:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" 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">
<!-- 创建一个person对象 -->
<bean id="person" class="com.xzd.test.Person">
<property name="personname">
<null/>
</property>
<property name="age" value="18"></property>
</bean>
</beans>

在这里插入图片描述

**<null/>**You can directly ask the property to assign a null 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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!-- 创建一个person对象 -->
    <bean id="person" class="com.xzd.test.Person">
    <property name="personname">
    <value><![CDATA[@#$*""&*&*]]]></value>
    </property>
    <property name="age" value="18"></property>
    </bean>
    </beans>
    

在这里插入图片描述

如何使用**<![CDATA[ Assign special symbols ]]>**

注入bean

注入内部bean

In fact, attributes are in addition to simple strings or numbers,But some object properties,本身就是一个对象,比如这样

package com.xzd.test;
public class Person {

String personname;
int age;
public void setPersonname(String personname) {

this.personname = personname;
}
public void setAge(int age) {

this.age = age;
}
}
package com.xzd.test;
public class Student {

String school;
Person person;
// 两
public Student(String school, Person person) {

this.school = school;
this.person = person;
}
@Override
public String toString() {

return "Student={\"name\":" + person.personname + ",\"age\":" + person.age + ",\"school\":"+this.school+"}";
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" 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">
<bean id="student" class="com.xzd.test.Student">
<constructor-arg name="school" value="游戏学院"></constructor-arg>
<constructor-arg name="person">
<!-- The so-called internal injection,就是在beanInject one inside the tagbean-->
<bean id="person" class="com.xzd.test.Person">
<property name="personname" value="张三"></property>
<property name="age" value="18"></property>
</bean>
</constructor-arg>
</bean>
</beans>
public class test {

public static void main(String[] args) {

ApplicationContext applicationContext= new ClassPathXmlApplicationContext("personbean.xml");
Student student= applicationContext.getBean("student", Student.class);//
System.out.println(student.toString());
}
}

在这里插入图片描述

注入外部bean

For example, there is businessservice中引用dao的方法一样.In a word, it is injecting an object class into an object class ,Front internal injection,在beanApply one to the labelbean标签,But the external injection type twobeanLabels are not applied,Instead, it is imported through attributes,So this is external injection.如下演示:

package com.xzd.test.service;
import com.xzd.test.dao.SaveDao;
public class SaveService {

SaveDao saveDao;
public void setSaveDao(SaveDao saveDao) {

this.saveDao = saveDao;
}
public void save() {

System.out.println("service调用dao");
saveDao.save();
}
}
package com.xzd.test.dao;
public class SaveDao {

public void save(){

System.out.println("daoadded to the database");
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" 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">
<bean id="SaveService" class="com.xzd.test.service.SaveService">
<property name="saveDao" ref="saveDao"></property>
</bean>
<bean id="saveDao" class="com.xzd.test.dao.SaveDao">
<!-- Can also be heredao的属性赋值-->
</bean>
</beans>
public class test {

public static void main(String[] args) {

ApplicationContext applicationContext= new ClassPathXmlApplicationContext("servicetest.xml");
SaveService saveService= applicationContext.getBean("SaveService", SaveService.class);//
saveService.save();
}
}

可以看结果

在这里插入图片描述

In fact, it can be seenproperty标签中,通过rel为name赋值,而relThe value of is created to introducebean中id即可.

注入集合数据

Inject plain data

毕竟有些bean的属性值,There may also be collections,比如数字,list,map等

package com.xzd.test;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class CollectionTest {

String arr[];
Map map;
Set set;
List list;
public void setArr(String[] arr) {

this.arr = arr;
}
public void setMap(Map map) {

this.map = map;
}
public void setSet(Set set) {

this.set = set;
}
public void setList(List list) {

this.list = list;
}
public void testToString(){

System.out.println("arr: "+this.arr);
System.out.println("set: "+this.set);
System.out.println("map: "+this.map);
System.out.println("list: "+this.list);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" 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">
<bean id="collectiontest" class="com.xzd.test.CollectionTest">
<property name="arr">
<!-- 因为数组,所以无法使用valueproperty or use it directlyvalue标签 do as follows-->
<array>
<value>"a"</value>
<value>"b"</value>
<value>"c"</value>
</array>
</property>
<property name="list">
<list>
<value>1</value>
<value>2</value>
<value>3</value>
</list>
</property>
<property name="map">
<map>
<entry key="name" value="张三"></entry>
<entry key="age" value="18"></entry>
</map>
</property>
<property name="set">
<set>
<value>"A"</value>
<value>"B"</value>
</set>
</property>
</bean>
</beans>
public class test {

public static void main(String[] args) {

ApplicationContext applicationContext= new ClassPathXmlApplicationContext("collectiontest.xml");
CollectionTest collectionTest= applicationContext.getBean("collectiontest", CollectionTest.class);//
collectionTest.testToString();
}
}

在这里插入图片描述

Supplementary injectionutil

package com.xzd.test;
import java.util.List;
public class CollectionTest {

List<String> list;
public void setList(List<String> list) {

this.list = list;
}
@Override
public String toString() {

return "CollectionTest{" +
"list=" + list +
'}';
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!--这里可以看出utilMake the following settings for the header-->
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
<bean id="collectiontest" class="com.xzd.test.CollectionTest">
<!-- can be imported like this -->
<property name="list" ref="s_list"></property>
</bean>
<util:list id="s_list">
<value>老头环</value>
<value>战神</value>
<value>曙光2</value>
</util:list>
</beans>
public class test {

public static void main(String[] args) {

ApplicationContext applicationContext= new ClassPathXmlApplicationContext("collectiontest.xml");
CollectionTest collectiontest= applicationContext.getBean("collectiontest", CollectionTest.class);
System.out.println(collectiontest.toString());
}
}

在这里插入图片描述

It can be seen that the following settings can be made,通过util标签进行设置.

Inject collection with bean

package com.xzd.test;
public class Course {

String courseName;
String teacherName;
public Course(String courseName, String teacherName) {

this.courseName = courseName;
this.teacherName = teacherName;
}
@Override
public String toString() {

return "{name:"+this.courseName+" teachername: "+this.teacherName+"}";
}
}
public class Student {

String name;
List<Course> listcourse;
public void setName(String name) {

this.name = name;
}
public void setListcourse(List<Course> listcourse) {

this.listcourse = listcourse;
}
public void testToString(){

System.out.println("name: "+this.name+"list: "+this.listcourse);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" 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">
<bean id="student" class="com.xzd.test.Student">
<property name="name" value="张三"></property>
<property name="listcourse" >
<list>
<!-- This collection is put into the importedbean-->
<ref bean="course1"></ref>
<ref bean="course2"></ref>
</list>
</property>
</bean>
<bean id="course1" class="com.xzd.test.Course">
<constructor-arg name="courseName" value="The old man's ring was cleared without damage"></constructor-arg>
<constructor-arg name="teacherName" value="宫崎老贼"></constructor-arg>
</bean>
<bean id="course2" class="com.xzd.test.Course">
<constructor-arg name="courseName" value="vSociety does not know mathematics especially three"></constructor-arg>
<constructor-arg name="teacherName" value="unprofitablev胖"></constructor-arg>
</bean>
</beans>
public class test {

public static void main(String[] args) {

ApplicationContext applicationContext= new ClassPathXmlApplicationContext("student.xml");
Student student= applicationContext.getBean("student", Student.class);
student.testToString();
}
}

在这里插入图片描述

copyright:author[little pig],Please bring the original link to reprint, thank you. https://en.javamana.com/2022/266/202209230842491484.html