1、spring的IOC
生活随笔
收集整理的這篇文章主要介紹了
1、spring的IOC
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
完整代碼:spring1.zip - 藍奏云
將對象的創(chuàng)建權交給spring框架,我們再使用new創(chuàng)建對象。
我們使用idea和maven進行編譯開發(fā)
1、導入spring相關的包
<dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.3.12</version></dependency>2、定義個Category類
package com.how2j.pojo;public class Category {public int getId() {return id;}public void setId(int id) {this.id = id;}@Overridepublic String toString() {return "Category{" +"id=" + id +", name='" + name + '\'' +'}';}public String getName() {return name;}public void setName(String name) {this.name = name;}private int id;private String name; }3、編寫xml文件,設置java bean,
<?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 name="c" class="com.how2j.pojo.Category"><property name="id" value="1"/><property name="name" value="category 1"/></bean> </beans>4、測試spring
package test; import com.how2j.pojo.Category; import org.junit.jupiter.api.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;public class TestSpring {@Test//spring的控制翻轉public void test1(){ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml" );Category c = (Category) context.getBean("c");System.out.println(c);} }注:目錄結構如下:
?
?
《新程序員》:云原生和全面數(shù)字化實踐50位技術專家共同創(chuàng)作,文字、視頻、音頻交互閱讀總結
以上是生活随笔為你收集整理的1、spring的IOC的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java控制台如何输入一行、多行?
- 下一篇: 2.1、spring属性注入-Set方法