spring

Somebody is alive, but he’s dead;

spring特性:1

Ioc控制反转:
	Inversion of control
	通俗的讲是将对象的创建销毁这个工作交给机器去做。
	这样做的好处就是省力,你自己就不用为了使用某个对象中的方法而到处创建对象了。控制反转是一种思想[^2]

maven:pom.xml中 command+N快捷添加以来spring-content,option+回车快捷补充变量
然后去资源目录下创建spring config-》applicationContent

// pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>spring01</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.3.16</version>
        </dependency>
    </dependencies>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>
</project>
// applicationContent.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="user" class="org.User" ></bean>
</beans>
// 这里就展示了ioc控制反转,User类就随便写个类测试一下,可以看到这里连我的user类包都没导就可以直接用了。
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class main {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext cpx = new ClassPathXmlApplicationContext("applicationContent.xml");
        Object user = cpx.getBean("user");

        System.out.println(user.toString());
    }
}

属性注入,方法注入
属性注入就是在刚才的applicationcontent基础上添加上属性,方法注入主要注意的是他的应用场景:在某些无法获取对象只能获取其方法的场景。(同时还有复杂注入这个后面用到再补充)

// 方法注入
    <bean class="org.User" factory-method="getInstance" id="xxx"></bean>

xml注入和java配置(注释)
以上学的都是将bean注入到spring容器里,注释最好用于固定类型注入,因为如果后面要改需要改源代码,进而需要重新编译。而xml就不需要重新编译,就适用于灵活一点的注入,会随时发生更改的。

// sayhi类
package org;

public class sayhi {
    public String sayhi(String name){
        return "hi"+name;
    }
}


// 相当于applicationcontent.xml. @configuration就相当于告诉他你是配置文件
package org;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class config {
    @Bean
    sayhi sayhi(){
        return new sayhi();
    }
}


// 测试
        AnnotationConfigApplicationContext act = new AnnotationConfigApplicationContext(config.class);
        sayhi sayhi = act.getBean("sayhi", sayhi.class);
        String d = sayhi.sayhi("d");
        System.out.println(d);

I‘d rather Succeed or die;


  1. spring代表Spring框架和Spring家族。 ↩︎

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值