spring的使用

什么是spring

spring是javaweb开发的一个框架,为了解决企业应用开发的复杂性。

它有几个重要的特性

  • IoC
  • DI
  • AOP

如何使用

IDEA中集合成了spring,可以直接在里面创建,引入相关的依赖

<dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.3.2</version>
        </dependency>
</dependencies>

在spring当中,不用new来获得对象,而是通过:

ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
UserService userServiceImpl = (UserService) context.getBean("userServiceImpl");

获得实现类。

beans.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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="..." class="...">  (1) (2)
        <!-- collaborators and configuration for this bean go here -->
    </bean>

    <bean id="..." class="...">
        <!-- collaborators and configuration for this bean go here -->
    </bean>

    <!-- more bean definitions go here -->

</beans>

内部属性的赋值也是通过内部传值来完成

    private UserDao userDao;

    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

此外还需要在bean.xml中使用ref属性
ref类似往userDao这里属性里面传入id="**“的对象,(id=” "可以类比于一个对象)。
注意!带属性的Impl类必须带有属性的set方法,如上所示,不然这个ref是起不了作用的

 <bean id="userServiceImpl" class="com.earth.service.UserServiceImpl">
        <property name="userDao" ref="oracle"/>
 </bean>

最后再解释下bean中的id、class属性
id:类比成new出来的对象名
class:要new的类名

 <bean id="user" class="com.earth.dao.UserDaoImpl"/>
    <bean id="mysql" class="com.earth.dao.UserDaoMysqlImpl"/>
    <bean id="oracle" class="com.earth.dao.UserDaoOracleImpl"/>
    <bean id="sqlServer" class="com.earth.dao.UserDaoSqlServerImpl"/>

    <bean id="userServiceImpl" class="com.earth.service.UserServiceImpl">
        <property name="userDao" ref="oracle"/>
    </bean>

控制翻转IoC(inversion of control)的对象如何创建

  • 无参构造
  • 有参构造

无参构造的类在通过context创建时,会调用类中的无参构造,在beans.xml中是这么赋值:

    <bean id="user" class="com.earth.pojo.User">
        <property name="id" value="1"/>
        <property name="name" value="earth地球"/>
    </bean>

有参构造的类需要通过<constructor-arg>标签来赋值
这个标签还有

  • index
  • type
  • name
    等属性,不写的默认按下标1、2、3。。。。方式递增,赋值方式如下:
    <bean id="house" class="com.earth.pojo.House">
        <constructor-arg value="i am a house"/>
        <constructor-arg value="1111"/>
        <constructor-arg ref="user1"/>
        <constructor-arg ref="user2"/>
    </bean>

依赖注入DI(depedency injection)

基础类型直接用value赋值,引用类型要用ref赋值
这些都是通过setter方法注入

    <bean id="user" class="com.earth.pojo.User">
        <constructor-arg value="galaxy"/>
        <constructor-arg value="123"/>
    </bean>

    <bean id="house" class="com.earth.pojo.House">
        <constructor-arg value="galaxy"/>
        <constructor-arg value="1000000"/>
        <constructor-arg ref="user"/>
        <constructor-arg ref="user"/>
    </bean>

    <bean id="student" class="com.earth.pojo.Student">

        <property name="name" value="earth地球"/>

        <property name="books">
            <array>
                <value>java</value>
                <value>c语言</value>
                <value>Python</value>
            </array>
        </property>

        <property name="card">
            <map>
                <entry key="身份证" value="1234567"/>
                <entry key="银行卡" value="7654321"/>
            </map>
        </property>

        <property name="games">
            <set>
                <value>civilization</value>
                <value>hearthstone</value>
                <value></value>
            </set>
        </property>

        <property name="hobby">
            <list>
                <value>奶油</value>
                <value>面包</value>
                <value>好好吃</value>
            </list>
        </property>

        <property name="house" ref="house"/>

        <property name="wife">
            <null></null>
        </property>

        <property name="info">
            <props>
                <prop key="学号">123456</prop>
                <prop key="姓名">earth地球</prop>
            </props>
        </property>
    </bean>

spring配置

  • 别名
  • 导入
  • name

bean的作用域

ScopeDescription
singleton(默认)将每个 Spring IoC 容器的单个 bean 定义范围限定为单个对象实例。
prototype将单个 bean 定义的作用域限定为任意数量的对象实例。
request将单个 bean 定义的范围限定为单个 HTTP 请求的生命周期。也就是说,每个 HTTP 请求都有一个在单个 bean 定义后面创建的 bean 实例。仅在可感知网络的 Spring ApplicationContext中有效。
session将单个 bean 定义的范围限定为 HTTP Session的生命周期。仅在可感知网络的 Spring ApplicationContext上下文中有效。
application将单个 bean 定义的范围限定为ServletContext的生命周期。仅在可感知网络的 Spring ApplicationContext上下文中有效。
websocket将单个 bean 定义的范围限定为WebSocket的生命周期。仅在可感知网络的 Spring ApplicationContext上下文中有效。

自动装配

通过@AutoWired注解

使用注解开发

首先要引入带有注解的.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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">

    <context:annotation-config/>

</beans>

然后类的属性定义时候使用注解,此时就无需在xml文件中编写注解。只需在pojo类中使用注解,如下所示:

public class Person {

    @Autowired
    private Cat cat;

    @Autowired
    private Dog dog;

    public void getPets(){
        cat.bark();
        dog.bark();
    }

    public void setCat(Cat cat) {
        this.cat = cat;
    }

    public void setDog(Dog dog) {
        this.dog = dog;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring框架使用了多种设计模式来实现其功能和特性。以下是一些Spring框架中常用的设计模式: 1. 单例模式:Spring框架中的Bean默认是单例的,即在整个应用程序中只有一个实例。这样可以提高性能并减少资源消耗。 2. 工厂模式:Spring框架使用工厂模式来创建和管理Bean。通过配置文件或注解,Spring可以根据需要动态地创建和管理Bean的实例。 3. 代理模式:Spring框架使用代理模式来实现AOP(面向切面编程)。通过代理模式,Spring可以在不修改原始类的情况下,为其添加额外的功能,如日志记录、事务管理等。 4. 观察者模式:Spring框架中的事件机制使用了观察者模式。通过定义事件和监听器,Spring可以在特定事件发生时通知相关的监听器执行相应的操作。 5. 模板方法模式:Spring框架中的JdbcTemplate和HibernateTemplate等模板类使用了模板方法模式。这些模板类提供了一些通用的操作方法,开发人员只需要实现特定的回调方法即可。 6. 适配器模式:Spring框架中的适配器模式用于将不同的接口转换为统一的接口。例如,Spring MVC中的HandlerAdapter将不同类型的控制器适配为统一的处理器接口。 7. 依赖注入模式:Spring框架使用依赖注入模式来管理对象之间的依赖关系。通过依赖注入,Spring可以将对象之间的依赖关系外部化,使得对象之间的耦合度降低。 8. 策略模式:Spring框架中的策略模式用于实现不同的算法或行为。例如,Spring Security中的认证策略和授权策略可以根据具体的需求进行配置和替换。 以上是Spring框架中常用的设计模式。通过使用这些设计模式,Spring框架能够提供灵活、可扩展和易于维护的应用程序开发环境。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值