Spring开发bean的基础应用

本文介绍Spring框架的基本概念、特点及使用流程,详细讲解IOC控制反转和DI依赖注入等核心概念,并探讨bean的实例化创建方式及依赖注入的多种方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Spring开发bean的基础应用

1.Spring是什么

1.翻译:春天
  特殊含义:JavaEE开发者的春天
2.是一个一站式的分层结构 轻量级开发框架
 目前最受欢迎的的框架
3.特点:开源
        轻量级(体积小)
        分层架构(按需添加)
        一站式(对目前的框架支持非常的高)
        在javaEE三层结构中,每一层都提供不同的解决技术
        IOC 控制反转,不需要new对象直接通过spring管理
        DC
        是个管理容器
核心:AOP IOC

4.用处:
  降低耦合性
  高性能
  易测试 junit
  声明式事物
5.使用流程

5.使用流程
  1.下载jar包
  2.导入核心包 beans Context expression core logging(日志信息)
  3.创建编写配置文件
    创建对象给sqring来管理
  4.创建ApplicationContext容器
  5.通过getBean来获取对象 如果对象不为空 则spring搭建成功
  6.导入日志包commons-logging-1.2.jar
2.项目Person对象没有显示的创建 到底是怎么来的
1.核心思想
IOC:
    inverse of control 反转控制
    就是把对象的创建反转(交给)spring 来管理
    之前是手动new对象 现在是向Spring拿来对象
    实现原理通过反射 + 配置文件来达到
    Class c = Class.forName("com.lanou.Person");
    Object o = C.newIncetance()
    context.put("Person",o);
    context.get("person");
DI技术
    dependcy injection
    依赖      注入
    类A 中需要B类提供的功能
    称为A依赖B
    例如:Service 依赖 DAO
    //UserDao dao = new UserDaoimplForJDBC();
    //UserDao dao = new UserDaoimplForHibernate();
    使用Spring之后
    UserDao dao = contex.getBean("userDAOimpl");
    //不需要修改源代码就能实现组件的切换
    注入:从外部把需要的对象放到内容部就叫注入
    UserService
    private UserDao ado;
    set and get
    依赖注入最终目的就是提高城乡的扩展性 尽可能不全修改源代码
3.bean的实例化创建方式
1.构造函数创建(默认)
2.静态工厂创建(调用指定的静态方法获取bean)
3.实例工厂创建(调用实例方法获取bean)
4.反射
4.依赖注入的四种方式
1.set注入(就是通过set方法 所有必须set方法)
2.构造函数注入
3.<p:命名空间
3.SpEL
5.ApplicationContext 两个实现类
ClassPathXMLApp理财听Context 用于加载class路径下的配置文件
FileSystemXMLApplicationContext 用于加载系统路径下的配置文件
6.两种容器
XMLBeanFactory 获取bean时才创建
Application    加载配置时就创建
7.bean元素的属性
1.Scope
    singleton 单例(默认)
    prototype  多例
    request    与Request生命中期一致
    session    与session生命周期一致
init-method    对象创建完了立即执行
destroy-method  对象销毁前执行
id              和name作用相同

2.复杂类型的注入
    array  使用array子标签 
           例如: <array>
           <value>123</value>,<value>456</value>
                </array>
    list 使用list子标签 同array
    map  使用entry子标签
         例如: <map>
            <entry key="XX" value=""></entry>
            <entry key-ref="引用类型" value=""></entry>
              </map>
    properties 使用props子标签
        例如: <props>
            <prop key="xx">value</prop>
            </props>
3.配置文件的模块化
    当一个配置文件太多是 可以将其按照功能模块划分
    1.在创建容器是 传入多个字符串对象(配置文件名)
    2.在配置文件使用import导入
8.Spring实现跟随项目的启动而启动 跟随项目的停止一并停止
实现步骤:
    1.在web.xml中配置监听器 使得项目启动时Spring也能一起启动
       类名org.springframework.web.context.ContextLoaderListener
       当监听到应用启动时会创建Spring器并放到ApplicationContext域中
    2.WebApplicationContextUtils 工具用来从WEBAppplication中取出Spring容器
    基础包
    commons-logging-1.1.3.jar
    spring-beans-4.3.8.RELEASE.jar
    spring-context-4.3.8.RELEASE.jar
    spring-core-4.3.8.RELEASE.jar
    spring-expression-4.3.8.RELEASE.jar

1.测试spring

bean

public class User {
    private String name;
    private String pwd;
    private Car car;
    private List<Order> orders;
    private Map info;
    private Properties properties;

    public Properties getProperties() {
        return properties;
    }
    public void setProperties(Properties properties) {
        this.properties = properties;
    }
    public Map getInfo() {
        return info;
    }
    public void setInfo(Map info) {
        this.info = info;
    }

    public class Order {
    private String ordername;
    private String price;
    public String getOrdername() {
        return ordername;
    }
    public void setOrdername(String ordername) {
        this.ordername = ordername;
    }

    public class Car {
    private String carname;
    private String price;

    public Car() {
        super();
        // TODO Auto-generated constructor stub
    }
    public Car(String carname, String price) {
        super();
        this.carname = carname;
        this.price = price;
    }
    @Override
    public String toString() {
        return "Car [carname=" + carname + ", price=" + price + "]";
    }
public class Test01 {
    public static void main(String[] args) {
//      //控制反转 你需要自己创建 你来找我要
//      User user = new User();
//      user.setName("呵呵呵");
//      user.setPwd("123");
//      user.sayMe();
        //IOC容器
        ApplicationContext ac = new ClassPathXmlApplicationContext("application2.xml");
        User user1 = (User) ac.getBean("user1");
        System.out.println(user1);
        user1.sayMe();

        User user2 = (User) ac.getBean("users");
        System.out.println(user2);
        user2.sayMe();

        Order order = (Order) ac.getBean("order1");
        System.out.println(order);


    }
}

application2.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:util="http://www.springframework.org/schema/util"
    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
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd">
<!-- IOC控制反转 DI依赖注入 -->
<!-- AOP面向切面编程 OOP -->
    <bean id="user1" class="com.lanou.entity.User">
        <property name="name" value="lck"></property>
        <property name="pwd" value="123"></property>
        <property name="car" ref="car1"></property>
    </bean>
    <bean id="user2" class="com.lanou.entity.User">
        <property name="name" value="lck2"></property>
        <property name="pwd" value="123"></property>
    </bean>
    <bean id="car1" class="com.lanou.entity.Car">
        <property name="carname" value="兰博基尼"></property>
        <property name="price" value="10"></property>
    </bean>

    <bean id="users" class="com.lanou.entity.User">
        <property name="name" value="haha"></property>
        <property name="orders">
            <list>
            <!-- Order2不能够被外界使用 -->
                <bean id = "order2" class="com.lanou.entity.Order">
                    <property name="ordername" value="gggg"></property>
                    <property name="price" value="14"></property>
                </bean>
                <ref bean="order1"/>
            </list>
        </property>
        <property name="info">
            <map>
                <entry key="idcard" value="1234"></entry>
                <entry key="parent" value-ref="user1"></entry>
            </map>
        </property>
        <property name="properties">
            <props>
                <prop key="nikeanme">yyy</prop>
                <prop key="age">5</prop>
            </props>
        </property>
    </bean>
    <bean id = "order1" class="com.lanou.entity.Order">
        <property name="ordername" value="waiyo"></property>
        <property name="price" value="1234"></property>
    </bean>

</beans>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值