首页 > 编程语言 > 详细

Spring(1)

时间:2018-04-15 23:16:35      阅读:192      评论:0      收藏:0      [点我收藏+]

Spring框架:由程序员自己new个构造方法来调用对象,变成由Spring创建对象并注入相关值,直接可以使用。

使用方法:

导入Spring相关的包

1.写一个类Category

2.写一个配置文件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:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    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 name="c" class="com.how2java.pojo.Category">//通关关键字c能获取Category对象
        <property name="name" value="category 1" />//在对象获取的时候注入value的值到name中
    </bean>
  
</beans>
 
3.调用
 ApplicationContext context = new ClassPathXmlApplicationContext(
                new String[] { "applicationContext.xml" });
 
        Category c = (Category) context.getBean("c");
 

Spring(1)

原文:https://www.cnblogs.com/lspa/p/8850082.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!