spring配置Dem

xiaoxiao2021-02-28  128

   

    一。在eclipse新建项目,导入jar包

     

 二 在XML内的配置

   <context-param>    <param-name>contextConfigLocation</param-name>

    //或都 这样 <param-value>classpath*:config/spring*.xml</param-value>    <param-value>classpath:</param-value>   </context-param>   <listener>    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>   </listener>

  三。创建一个bean

     public class UserDemo { private int id; private String name; public UserDemo(int id, String name) { super(); this.id = id; this.name = name; } public UserDemo() { super(); // TODO Auto-generated constructor stub } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }

四。在applicationContext.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" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop"    xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd  http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">  <bean id="user" class="test.UserDemo">   <property name="id" value="12"></property>    <property name="name" value="name"></property>  </bean> </beans> 

  五 测试连接是否成功

      import org.junit.Test;     import org.springframework.context.ApplicationContext;     import org.springframework.context.support.ClassPathXmlApplicationContext;     public class test { private ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");   @Test public  void test(){ UserDemo userDemo=(UserDemo) ac.getBean("user"); System.out.println(userDemo.getId()+userDemo.getName()); } }

转载请注明原文地址: https://www.6miu.com/read-65568.html

最新回复(0)