最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Spring零基礎(chǔ)入門IOC

 更新時(shí)間:2022年08月09日 09:18:32   作者:進(jìn)擊攻城獅  
IoC就是比方說有一個(gè)類,我們想要調(diào)用類里面的方法(不是靜態(tài)方法),就要?jiǎng)?chuàng)建該類的對(duì)象,使用對(duì)象調(diào)用方法來實(shí)現(xiàn)。但對(duì)于Spring來說,Spring創(chuàng)建對(duì)象的過程,不是在代碼里面實(shí)現(xiàn)的,而是交給Spring來進(jìn)行配置實(shí)現(xiàn)的

1.HelloSpring

導(dǎo)入Jar包

  <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>5.2.0.RELEASE</version>
        </dependency>

1、編寫一個(gè)Hello實(shí)體類

public class Hello {
    private String str;
    public Hello(String str) {
        this.str = str;
    }
    public Hello() {
    }
    public String getStr() {
        return str;
    }
//set方法是核心
    public void setStr(String str) {
        this.str = str;
    }
    @Override
    public String toString() {
        return "Hello{" +
                "str='" + str + '\'' +
                '}';
    }
}

2、編寫我們的spring文件 , 這里我們命名為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="hello" class="com.hc.pojo.Hello">
       <property name="str" value="Spring"/>
    </bean>
<!--    使用spring來創(chuàng)建對(duì)象,在spring這些都稱為Bean-->
<!--    類型   變量名 =new 類型();-->
<!--    Hello hello=new Hello();-->
</beans>

3、我們可以去進(jìn)行測(cè)試了 .

public class MyTest {
    public static void main(String[] args) {
       ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        Object hello = context.getBean("hello");
        System.out.println(hello.toString());
    }
}

思考

  • Hello 對(duì)象是誰創(chuàng)建的 ? hello 對(duì)象是由Spring創(chuàng)建的
  • Hello 對(duì)象的屬性是怎么設(shè)置的 ? hello 對(duì)象的屬性是由Spring容器設(shè)置的

這個(gè)過程就叫控制反轉(zhuǎn) :

  • 控制 : 誰來控制對(duì)象的創(chuàng)建 , 傳統(tǒng)應(yīng)用程序的對(duì)象是由程序本身控制創(chuàng)建的 , 使用Spring后 , 對(duì)象是由Spring來創(chuàng)建的
  • 反轉(zhuǎn) : 程序本身不創(chuàng)建對(duì)象 , 而變成被動(dòng)的接收對(duì)象 .

依賴注入 : 就是利用set方法來進(jìn)行注入的.

IOC是一種編程思想,由主動(dòng)的編程變成被動(dòng)的接收

可以通過newClassPathXmlApplicationContext去瀏覽一下底層源碼 .

修改案例一

我們?cè)诎咐恢校?新增一個(gè)Spring配置文件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="mysqlImpl" class="com.hc.dao.UserDaoMysqlImpl"/>
    <bean id="OracleImpl" class="com.hc.dao.UserDaoOracleImpl"/>
    <!--    使用spring來創(chuàng)建對(duì)象,在spring這些都稱為Bean-->
    <!--    類型   變量名 =new 類型();-->
    <!--    Hello hello=new Hello();-->
    <bean id="UserServiceImpl" class="com.hc.service.UserServiceImpl">
<property name="userDao" ref="OracleImpl"/>
    </bean>
</beans>

測(cè)試!

import com.hc.dao.UserDaoMysqlImpl;
import com.hc.dao.UserDaoOracleImpl;
import com.hc.dao.UserDaoSqlserverImpl;
import com.hc.service.UserService;
import com.hc.service.UserServiceImpl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import sun.security.mscapi.CPublicKey;
/**
 * @author HC
 * @version 1.0
 */
public class MyTest {
    public static void main(String[] args) {
        //用戶調(diào)用的是業(yè)務(wù)層,dao層他們不需要接觸!
//        UserServiceImpl userService = new UserServiceImpl();
//        userService.setUserDao(new UserDaoSqlserverImpl());
//        userService.setUserDao(new UserDaoOracleImpl());
//        userService.getUser();
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        UserServiceImpl userServiceImpl = (UserServiceImpl) context.getBean("UserServiceImpl");
        userServiceImpl.getUser();
    }
}

OK , 到了現(xiàn)在 , 我們徹底不用再程序中去改動(dòng)了 , 要實(shí)現(xiàn)不同的操作 , 只需要在xml配置文件中進(jìn)行修改 , 所謂的IoC,一句話搞定 : 對(duì)象由Spring 來創(chuàng)建 , 管理 , 裝配 !

2.IOC創(chuàng)建對(duì)象方式

2.1.通過無參構(gòu)造方法來創(chuàng)建

通過無參構(gòu)造方法來創(chuàng)建

1、User.java

public class User {
    private String name;
    public User(String name) {
        this.name = name;
    }
//    public User() {
//    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public void show()
    {
        System.out.println("name="+getName());
    }
    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                '}';
    }
}

2、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="user" class="com.kuang.pojo.User">-->
<!--       <property name="name" value="hc"/>-->
<!--   </bean>-->
<!--    //二、有參-->
<!--    <bean id="user" class="com.kuang.pojo.User">-->
<!--       <constructor-arg index="0" value="狂神說java"/>-->
<!--    </bean>-->
<!--    三、有參-->
<!--    <bean id="user" class="com.kuang.pojo.User">-->
<!--        <constructor-arg name="name" value="hc"/>-->
<!--    </bean>-->
    <bean id="user" class="com.kuang.pojo.UserT" name="u1 u2,u3">
        <property name="name" value="西部開源"/>
    </bean>
<!--    alias取別名-->
<!--    <alias name="user" alias="ccccccccccccccccc"/>-->
</beans>

3、測(cè)試類

public class MyTest {
    public static void main(String[] args) {
       // User user = new User();
       ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
       //User user1 = (User) context.getBean("user");
       // User user1 = (User) context.getBean("ccccccccccccccccc");
        UserT user4 = (UserT) context.getBean("u2");
        // User user2 = (User) context.getBean("user");
       user4.show();
        //System.out.println(user1==user2);
    }
}

結(jié)果可以發(fā)現(xiàn),在調(diào)用show方法之前,User對(duì)象已經(jīng)通過無參構(gòu)造初始化了!

2.2.通過有參構(gòu)造方法來創(chuàng)建

通過有參構(gòu)造方法來創(chuàng)建

1、UserT . java

public class UserT {
    private String name;
    public UserT() {
        System.out.println("UserT被創(chuàng)建了");
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public void show()
    {
        System.out.println("name="+getName());
    }
}

2、beans.xml 有三種方式編寫

<!--一、無參-->
<!--   <bean id="user" class="com.kuang.pojo.User">-->
<!--       <property name="name" value="hc"/>-->
<!--   </bean>-->
<!--    //二、有參-->
<!--    <bean id="user" class="com.kuang.pojo.User">-->
<!--       <constructor-arg index="0" value="狂神說java"/>-->
<!--    </bean>-->
<!--    三、有參-->
<!--    <bean id="user" class="com.kuang.pojo.User">-->
<!--        <constructor-arg name="name" value="hc"/>-->
<!--    </bean>-->

3、測(cè)試

public class MyTest {
    public static void main(String[] args) {
       // User user = new User();
       ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
       //User user1 = (User) context.getBean("user");
       // User user1 = (User) context.getBean("ccccccccccccccccc");
        UserT user4 = (UserT) context.getBean("u2");
        // User user2 = (User) context.getBean("user");
       user4.show();
        //System.out.println(user1==user2);
    }
}

結(jié)論:在配置文件加載的時(shí)候。其中管理的對(duì)象都已經(jīng)初始化了!

到此這篇關(guān)于Spring零基礎(chǔ)入門IOC的文章就介紹到這了,更多相關(guān)Spring IOC內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

太仓市| 馆陶县| 大英县| 永兴县| 赤壁市| 大新县| 吴忠市| 门源| 仪陇县| 疏附县| 定襄县| 博罗县| 大理市| 普安县| 张家口市| 天柱县| 加查县| 景洪市| 门头沟区| 杭锦后旗| 涿鹿县| 石狮市| 娱乐| 繁峙县| 舟曲县| 蓬安县| 漳平市| 钟祥市| 曲阳县| 阳春市| 襄垣县| 玉门市| 洛阳市| 酉阳| 从江县| 鲜城| 龙川县| 石屏县| 定南县| 英超| 虹口区|