Spring基础知识

Spring的bean定义方式

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

    <bean id="dog" class="cc.anti.Dog"></bean>
    <bean id ="people" class="cc.anti.People" >
        <property name="dog" ref="dog"></property>
    </bean>
</beans>
package cc.anti;
public class Dog {
    @Override
    public String toString() {
        return "dog";
    }
}

package cc.anti;

import cc.anti.Cat;
import cc.anti.Dog;
public class People {
    private Dog dog;
    public void setDog(Dog dog) {
        this.dog = dog;
    }
    @Override
    public String toString() {
        return "people+" + cat.toString() +"+" +dog.toString();
    }
}

package cc.anti;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTest {

    @Test
    public void MyTest(){
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        People people = context.getBean("people",Cat.class);
        System.out.printf(cat.toString());
    }
}

javaConfig

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

    <!-- 开启自动扫描 -->
    <context:component-scan base-package="cc.anti.javaConfigTest"/>

</beans>
package cc.anti.javaConfigTest;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration //等价于 <beans> </beans>
public class House {
    @Bean //等价于 <bean> <bean>
    public Door door(){
        return new Door();
    }
    @Override
    public String toString() {
        return "house";
    }

}
package cc.anti.javaConfigTest;
public class Door {
    @Override
    public String toString() {
        return "door";
    }
}
public void MyJavaConfigTest(){
    ApplicationContext context = new ClassPathXmlApplicationContext("beans-javaConfig.xml");
    House house = context.getBean("house", House.class);
    System.out.printf(house.toString());
    Door door = context.getBean("door", Door.class);
    System.out.printf(door.toString());
}

注解

@Component : @Service@Controller

装配方式

非自动装配

见 Spring的bean定义方式-xml的例子;

自动装配

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
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd">
	<!-- autowire 开启自动装配-->
    <bean id="dog" class="cc.anti.Dog"></bean>
    <bean id ="people" class="cc.anti.People" autowire="byName"></bean>

</beans>
package cc.anti;
public class Dog {
    @Override
    public String toString() {
        return "dog";
    }
}

package cc.anti;

import cc.anti.Cat;
import cc.anti.Dog;
public class People {
    private Dog dog;
    public void setDog(Dog dog) {
        this.dog = dog;
    }
    @Override
    public String toString() {
        return "people+" + cat.toString() +"+" +dog.toString();
    }
}
public void MyAutoTest() {
    ApplicationContext context = new ClassPathXmlApplicationContext("beans-auto.xml");
    People people = context.getBean("people", People.class);
    System.out.printf(people.toString());
}

注解

<?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
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd">

    <!-- 开启注解-->
    <context:annotation-config/>

    <bean id="people" class="cc.anti.People" />
    <bean id="dog" class="cc.anti.Dog" />

</beans>
package cc.anti;

public class Dog {
    @Override
    public String toString() {
        return "dog";
    }
}
package cc.anti;

import cc.anti.Cat;
import cc.anti.Dog;
import org.springframework.beans.factory.annotation.Autowired;

public class People {
    @Autowired //注入方式
    private Dog dog;
    @Override
    public String toString() {
        return "people+" + cat.toString() +"+" +dog.toString();
    }
}
public void MyAutoAnnotateTest() {
    ApplicationContext context = new ClassPathXmlApplicationContext("beans-annotate.xml");
    People people = context.getBean("people", People.class);
    System.out.printf(people.toString());
}

依赖注入方式

setter注入

构造器注入

注解

Java Spring是一个广泛使用的轻量级开源框架,用于构建企业级Web应用程序。Spring框架提供了模块化的解决方案,简化了企业级应用的开发、管理以及依赖注入(Dependency Injection,DI)等功能。以下是一些Spring基础知识点的整理: 1. **IoC(Inversion of Control)和DI(Dependency Injection)**:Spring的核心思想就是IoC,它反转了传统的控制流动,使对象之间通过容器管理彼此的依赖关系,而不是硬编码。DI是IoC的一种具体实现方式,通过配置文件或注解自动为对象提供所需依赖。 2. **Bean的作用域和生命周期**:Spring中的Bean有多种作用域,如Singleton(单例)、Prototype(原型)、Request、Session等。每个Bean都有其生命周期,从创建、初始化到使用和销毁。 3. **Spring配置文件**:通常使用XML配置文件(如applicationContext.xml)或Java配置(@Configuration classes)来定义Spring应用的组件和依赖关系。 4. **AOP(Aspect Oriented Programming)**:Spring AOP支持面向切面编程,可以编写跨组件的行为,比如日志记录、事务管理等。 5. **Spring MVC**:Spring提供的web MVC架构,包括Controller处理HTTP请求,Model负责数据访问和业务逻辑,View负责渲染结果给用户。 6. **Spring Boot**:Spring Boot简化了Spring应用的初始搭建,自动配置了许多常用的功能,使得快速开发变得更容易。 7. **Spring Data**:提供了一套高级API,用于简化数据访问操作,如JPA、MongoDB等。 8. **Spring Security**:用于实现Web应用的安全管理,包括认证、授权、会话管理等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值