Spring根据XML配置文件 p名称空间注入属性的实例
2017-11-16 09:03
751 查看
要生成对象并通过名称空间注入属性的类 代码如下:
package com.swift; public class User { private String userName; public void setUserName(String userName) { this.userName = userName; } public String fun() { return "User's fun is ready."+this.userName; } }
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" 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"> <!-- IoC 控制反转 SpringSpring根据XML配置文件生成对象 --> <bean id="user" class="com.swift.User" p:userName="peach"></bean> </beans>
p:userName="peach"
p:后是属性的变量名 后面是赋值
约束是xmlns:p="http://www.springframework.org/schema/p"
生成对象及属性值调用方法,代码如下:
package com.swift; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; @WebServlet("/test") public class TestIOC extends HttpServlet { private static final long serialVersionUID = 1L; public TestIOC() { super(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.getWriter().append("Served at: ").append(request.getContextPath()); @SuppressWarnings("resource") //就是下边这几句了 ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml"); User user=(User) context.getBean("user"); String userInfo=user.fun(); response.getWriter().println(); response.getWriter().append(userInfo); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
以上这篇Spring根据XML配置文件 p名称空间注入属性的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。
您可能感兴趣的文章:
相关文章推荐
- Spring根据XML配置文件 p名称空间注入属性
- Spring根据XML配置文件注入属性 其实也是造bean,看看是使用constructor还是setter顺带完成属性赋值
- Spring根据XML配置文件注入对象类型属性
- Spring根据XML配置文件注入属性的方法
- Spring代码实例系列-09:通过Spring PropertyPlaceholderConfigurer将properties配置的属性注入到xml配置文件中
- [Spring][Bean]Spring配置文件、属性注入、从IOC容器获取实例
- Spring AMQP实例 以及Spring 配置文件动态注入属性使用实战
- spring 框架中的依赖注入(IOC--设值注入)---使用xml简单配置文件---的具体实例的简单实现
- Spring框架xml配置文件 复杂类型属性注入——数组 list map properties DI dependency injection 依赖注入——属性值的注入依赖于建立的对象(堆空间)
- Spring基于xml配置文件对list、set、map集合的属性注入
- Spring AOP注解通过@Autowired,@Resource,@Qualifier,@PostConstruct,@PreDestroy注入属性的配置文件详解
- Spring AOP注解通过@Autowired,@Resource,@Qualifier,@PostConstruct,@PreDestroy注入属性的配置文件详解
- Spring AOP注解通过@Autowired,@Resource,@Qualifier,@PostConstruct,@PreDestroy注入属性的配置文件详解
- Spring依赖注入的XML配置文件的实现思想(1)——准备工作:如何读取XML的文件的内容
- Spring AOP注解通过@Autowired,@Resource,@Qualifier,@PostConstruct,@PreDestroy注入属性的配置文件详解
- Spring AOP注解通过@Autowired,@Resource,@Qualifier,@PostConstruct,@PreDestroy注入属性的配置文件详解
- spring List,Set,Map,Properties,array的使用配置文件注入实例
- spring 将配置文件中的值注入 属性
- spring-自动加载配置文件\使用属性文件注入
- Spring AOP注解通过@Autowired,@Resource,@Qualifier,@PostConstruct,@PreDestroy注入属性的配置文件详解