spring配置

来源:互联网 发布:廊坊数控编程招聘信息 编辑:程序博客网 时间:2024/05/16 06:51

下载spring插件


点击下载

然后maven导入spring jar

<!-- 引入Spring依赖 -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>4.3.10.RELEASE</version>
    </dependency>



//如何使用

第一步

写一个实体类

Student


第二步

配置student

写一个spring.xml



配置: 

<!-- class是实体类的全限定名字-->

<bean id="student" class="com.zking.entity.Student">
   <property name="yx" ref="Yxintent">
      <!-- name实体类的属性名字 value是值 ref是导入一个对象(实体类)-->
   </property>
    <property name="mw" ref="Mwintent"></property>

        <property name="实体类的属性" value="值"></property>
     <property name="pf" ref="PfIntent"></property>
</bean>

执行:

两种方法

//1.加载spring配置文件
//1.1 延迟加载(懒加载)

<!-- 你写的xml getBean("student");是你之前配置的名字-->
BeanFactory bf=new XmlBeanFactory(new ClassPathResource("applicationcontext.xml"));

<!-- 你要执行的类-->
Student student=(Student) bf.getBean("student");
System.out.println(student);


//1.2及时加载
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
Person person=(Person) ac.getBean("person");




原创粉丝点击