大模型推理部署实战:从量化到连续批处理的性能优化指南
2026/10/22 9:16:10
alias标签
<!--别名,如果添加了别名,我们也可以使用别名获取到这个对象--><aliasname="user"alias="balbala"/>实例化容器的时候调用
Useruser=(User)context.getBean("balbala");实际上取别名不如用bean,不推荐使用这种方式起别名
<bean id="user2"class="com.cike3.pojo.User2"name="userT u2,u3;u4"><property name="name"value="cike_y"/></bean>这个import,一般用于团队开发使用,它可以将多个配置文件,导入合并为一个
假设,现在项目中有多个人开发,这三个人负责不同的类开发,不同的类需要注册的bean中,我们可以利用import,将所有人的beans.xml合并为一个总的!
<importresource="beans.xml"/><importresource="beans2.xml"/>使用的时候,直接使用总的配置
官方文档:
https://docs.spring.io/spring-framework/reference/core/beans/dependencies/factory-collaborators.html https://docs.spring.io/spring-framework/docs/5.2.0.RELEASE/spring-framework-reference/core.html#beans-constructor-injection前面 "IoC创建对象的方式“
主要有以下注入:
<beanid="student"class="com.cike4.pojo.Student"><!--第一种,普通值注入,直接使用value--><propertyname="name"value="cike_y"/></bean><beanid="address"class="com.cike4.pojo.Address"><propertyname="address"value="广东"/></bean><beanid="student"class="com.cike4.pojo.Student"><!--第二种,Bean注入,ref引用address容器id--><propertyname="address"ref="address"/></bean><beanid="student"class="com.cike4.pojo.Student"><!--数组注入--><propertyname="books"><array><value>十日终焉</value><value>凡人修仙传</value><value>夏日重现</value></array></property></bean><beanid="student"class="com.cike4.pojo.Student"><propertyname="hobbys"><list><value>打游戏</value><value>看电影</value><value>爱躺平</value></list></property></bean><beanid="student"class="com.cike4.pojo.Student"><!--Map 注入--><propertyname="card"><map><entrykey="身份证"value="444444444444444444"/></map></property></bean><beanid="student"class="com.cike4.pojo.Student"><!--Set--><propertyname="games"><set><value>和平精英</value><value>原神</value></set></property></bean><beanid="student"class="com.cike4.pojo.Student"><!-- null value默认不写为null <property name="wife" value=""/> --><propertyname="wife"><null/></property></bean><beanid="student"class="com.cike4.pojo.Student"><!--Properties--><propertyname="info"><props><propkey="学号">20230302222</prop><propkey="姓名">cike_y</prop><propkey="性别">男</prop><propkey="username">root</prop><propkey="password">123456</prop></props></property></bean>publicclassAddress{privateStringaddress;publicStringgetAddress(){returnaddress;}publicvoidsetAddress(Stringaddress){this.address=address;}}publicclassStudent{// 引用类型privateStringname;privateAddressaddress;privateString[]books;privateList<String>hobbys;privateMap<String,Object>card;privateSet<String>games;privateStringwife;privatePropertiesinfo;}<?xml version="1.0" encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd"><beanid="address"class="com.cike4.pojo.Address"><propertyname="address"value="广东"/></bean><beanid="student"class="com.cike4.pojo.Student"><!--第一种,普通值注入,直接使用value--><propertyname="name"value="cike_y"/><!--第二种,Bean注入,ref--><propertyname="address"ref="address"/><!--数组注入--><propertyname="books"><array><value>十日终焉</value><value>凡人修仙传</value><value>夏日重现</value></array></property><!--List注入--><propertyname="hobbys"><list><value>打游戏</value><value>看电影</value><value>爱躺平</value></list></property><!--Map 注入--><propertyname="card"><map><entrykey="身份证"value="444444444444444444"/></map></property><!--Set--><propertyname="games"><set><value>和平精英</value><value>原神</value></set></property><!-- null value默认不写为null <property name="wife" value=""/> --><propertyname="wife"><null/></property><!--Properties--><propertyname="info"><props><propkey="学号">20230302222</prop><propkey="姓名">cike_y</prop><propkey="性别">男</prop><propkey="username">root</prop><propkey="password">123456</prop></props></property></bean></beans>publicclasssprint_04_Test{@Testpublicvoidtest(){ApplicationContextcontext=newClassPathXmlApplicationContext("beans.xml");Studentstudent=(Student)context.getBean("student");System.out.println(student.toString());/* * Student{name='cike_y', * address=Address{address='广东'}, * books=[十日终焉, 凡人修仙传, 夏日重现], * hobbys=[打游戏, 看电影, 爱躺平], * card={身份证=444444444444444444}, * games=[和平精英, 原神], * wife='null', * info={ * 学号=20230302222, * 性别=男, * password=123456, * 姓名=cike_y, * username=root * } * } */}}官方文档:
https://docs.spring.io/spring-framework/docs/5.2.0.RELEASE/spring-framework-reference/core.html#beans-collection-elements https://docs.spring.io/spring-framework/reference/core/beans/dependencies/factory-properties-detailed.html注意:p命名空间和c命名空间的使用,需要导入xml约束
xmlns:p="http://www.springframework.org/schema/p"xmlns:c="http://www.springframework.org/schema/c"要用它一定要先导入官方文档中的p命名空间
xmlns:p="http://www.springframework.org/schema/p"它对应着set注入的一些方法
<!--p命名空间注入,可以直接注入属性的值:property--><beanid="user"class="com.cike4.pojo.User"p:name="cike_y"p:age="20"/><!--相当于 <bean id="user" class="com.cike4.pojo.User"> <property name="name" value="cike_y"/> <property name="age" value="20"/> </bean> -->也可以引用其他的Bean对象
userbeans.xml中
<?xml version="1.0" encoding="UTF-8"?><beansxmlns="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 https://www.springframework.org/schema/beans/spring-beans.xsd"><!--p命名空间注入,可以直接注入属性的值:property--><beanid="user"class="com.cike4.pojo.User"p:name="cike_y"p:age="20"/><!--相当于 <bean id="user" class="com.cike4.pojo.User"> <property name="name" value="cike_y"/> <property name="age" value="20"/> </bean> --></beans>JavaBean 的User类
publicclassUser{privateStringname;privateintage;publicStringgetName(){returnname;}publicvoidsetName(Stringname){this.name=name;}publicintgetAge(){returnage;}publicvoidsetAge(intage){this.age=age;}@OverridepublicStringtoString(){return"User{"+"name='"+name+'\''+", age="+age+'}';}}测试方法中实例化对象
@Testpublicvoidtest2(){ApplicationContextcontext=newClassPathXmlApplicationContext("userbeans.xml");// 这里申明了类型,就不需要强壮类型 User 类了Useruser=context.getBean("user",User.class);System.out.println(user.toString());}要先导入c命名空间
xmlns:c="http://www.springframework.org/schema/c"userbean.xml 中
<!--c命名空间注入,可以通过构造器注入:construct-args--><beanid="user2"class="com.cike4.pojo.User"c:name="user2"c:age="20"/><!--相当于 <bean id="user2" class="com.cike4.pojo.User"> <constructor-arg name="name" value="cike_y"/> <constructor-arg name="age" value="20"/> </bean> -->User类添加构造器方法
publicUser(){}publicUser(Stringname,intage){this.name=name;this.age=age;}测试方法
@Testpublicvoidtest2(){ApplicationContextcontext=newClassPathXmlApplicationContext("userbeans.xml");// 这里申明了类型,就不需要强壮类型 User 类了Useruser=context.getBean("user2",User.class);System.out.println(user.toString());}官方解释:
官方文档:
https://docs.spring.io/spring-framework/docs/5.2.0.RELEASE/spring-framework-reference/core.html#beans-p-namespace https://docs.spring.io/spring-framework/reference/core/beans/dependencies/factory-properties-detailed.html#beans-p-namespace