目錄
-
Spring簡介
-
Spring項目
-
Bean管理
-
基于xml的Bean管理
-
創(chuàng)建對象
-
屬性注入
-
-
基于xml+注解的Bean管理
-
創(chuàng)建對象
-
屬性注入
-
-
基于純注解的Bean管理
-
內(nèi)容
Spring簡介
-
Spring是什么
Spring是于2003 年興起的一個輕量級的Java的開放源代碼的設(shè)計層面框架。Spring解決的是業(yè)務(wù)邏輯層和其他各層的松耦合問題,因此它將面向接口的編程思想貫穿整個系統(tǒng)應(yīng)用。
-
Spring核心是什么
Spring的核心是控制反轉(zhuǎn)(IOC)和面向切面(AOP)。
-
IOC:控制反轉(zhuǎn),將創(chuàng)建對象的過程交給spring進行管理。
-
AOP:面向切面,在不修改源代碼的情況之下進行代碼功能的增強。
-
-
Spring優(yōu)勢是什么
Spring是為了解決企業(yè)應(yīng)用開發(fā)的復雜性而創(chuàng)建的。Spring框架的主要優(yōu)勢之一就是其分層架構(gòu),分層架構(gòu)允許使用者選擇使用哪一個組件,同時為 JavaEE 應(yīng)用程序開發(fā)提供集成的框架??偨Y(jié)下來分以下幾點:
-
方便解耦,簡化開發(fā),Spring就是一個大工廠,可以將所有對象創(chuàng)建和依賴關(guān)系維護,交給Spring管理,這也是IOC的作用。
-
AOP編程的支持,Spring提供面向切面編程,可以方便的實現(xiàn)對程序進行權(quán)限攔截、運行監(jiān)控等功能。
-
聲明式事務(wù)的支持,只需要通過配置就可以完成對事務(wù)的管理,而無需手動編程。
-
方便程序的測試,Spring對Junit4支持,可以通過注解方便的測試Spring程序。
-
方便集成各種優(yōu)秀框架,Spring不排斥各種優(yōu)秀的開源框架,其內(nèi)部提供了對各種優(yōu)秀框架(如:Struts2、Hibernate、MyBatis等)的直接支持。
-
降低JavaEE API的使用難度,Spring 對JavaEE開發(fā)中非常難用的一些API(JDBC、JavaMail等),都提供了封裝,使這些API應(yīng)用難度大大降低。
-
-
Bean是什么
Bean是Spring容器管理的對象。
-
Spring容器是什么
Spring容器是創(chuàng)建以及管理Bean對象的。
Spring項目
-
IDEA創(chuàng)建項目
-
配置Maven,使用默認版本也可以
-
導入spring和單元測試junit依賴
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>5.2.12.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.2.12.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>5.2.12.RELEASE</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency>
-
創(chuàng)建spring-config.xml(文件名隨便取)
Bean管理
-
創(chuàng)建Demo類
public class Demo { private String name; private Integer age; public Demo(){} public Demo(String name, Integer age) { this.name = name; this.age = age; } public void setName(String name) {this.name = name;} public void setAge(Integer age) {this.age = age;} @Override public String toString() { return "Demo{" + "name='" + name + '\'' + ", age=" + age + '}'; } }
-
創(chuàng)建測試類
import org.junit.Test; public class Main { @Test public void test(){ } }
基于xml的Bean管理
創(chuàng)建對象
-
spring配置文件(spring-config.xml)添加配置,注冊Demo類為Bean對象
<bean class="com.example.aji.bean.Demo" id="demo"> </bean>
-
從容器中取出Demo對象
-
ApplicationContext spring上下文
-
ClassPathXmlApplicationContext 以xml配置文件方式創(chuàng)建上下文
-
.getBean(beanName) 跟據(jù)beanName從上下文中獲取Bean對象
-
.getBean(class) 跟據(jù)class從上下文中獲取Bean對象
@Test public void test(){ ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml"); Demo demo = (Demo)context.getBean("demo"); System.out.println(demo); }
-
屬性注入
-
構(gòu)造器注入
- constructor-arg 通過構(gòu)造器注入屬性
<bean class="com.example.aji.bean.Demo" id="demo"> <constructor-arg name="name" value="aji"/> <constructor-arg name="age" value="20"/> </bean>
-
set方法注入
- property 通過set方法注入屬性
<bean class="com.example.aji.bean.Demo" id="demo"> <property name="name" value="aji"/> <property name="age" value="21"/> </bean>
基于xml+注解的Bean管理
-
注解
-
注解是代碼特殊標記,格式:@注解名稱(屬性名稱=屬性值,屬性名稱=屬性值...)
-
使用注解,注解作用在類上面,方法上面,屬性上邊
-
使用注解的目的:簡化XML配置
-
-
編輯Spring配置文件添加注解掃描功能
<!--開啟注解掃描 com.example.aji 所有的包中的所有的類--> <context:component-scan base-package="com.example.aji"/>
創(chuàng)建對象
-
Spring針對Bean管理中創(chuàng)建對象提供的注解
-
@Component 普通的類
-
@Controller 表現(xiàn)層
-
@Service 業(yè)務(wù)層
-
@Repository 持久層
-
-
創(chuàng)建普通對象
import org.springframework.stereotype.Component; @Component public class Demo { private String name; private Integer age; public Demo(){} public Demo(String name, Integer age) { this.name = name; this.age = age; } @Override public String toString() { return "Demo{" + "name='" + name + '\'' + ", age=" + age + '}'; } }
-
創(chuàng)建業(yè)務(wù)層對象
-
創(chuàng)建DemoService接口
public interface DemoService { Demo getDemo(); }
-
創(chuàng)建DemoServiceImpl實現(xiàn)DemoService接口變更添加
import com.example.aji.bean.Demo; import com.example.aji.service.DemoService; import org.springframework.stereotype.Service; @Service(value = "demoService") public class DemoServiceImpl implements DemoService { @Override public Demo getDemo() { return new Demo("aji",20); } }
-
-
其他對象也類似,添加相應(yīng)注解即可
屬性注入
-
屬性注入注解
-
@Value 用于注入普通類型(String,int,double等類型)
-
@Autowired 默認按類型進行自動裝配(引用類型)
-
@Qualifier 不能單獨使用必須和@Autowired一起使用,強制使用名稱注入
-
@Resource Java提供的注解,也被支持。使用name屬性,按名稱注入
-
-
@Value
@Component public class Demo { @Value("aji") private String name; @Value(value = "21") private Integer age; @Override public String toString() { return "Demo{" + "name='" + name + '\'' + ", age=" + age + '}'; } }
-
@Autowired
import com.example.aji.bean.Demo; import com.example.aji.service.DemoService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service(value = "demoService") public class DemoServiceImpl implements DemoService { @Autowired private Demo demo; @Override public Demo getDemo() { return demo; } }
基于純注解的Bean管理
-
刪除spring-config.xml文件,并創(chuàng)建SpringConfig類,添加@Component注解把該類交給spring處理,添加@ComponentScan注解指定spring掃描的包路徑
@Component @ComponentScan("com.example.aji") public class SpringConfig { }
-
測試(其余部分與xml+注解方式一致)文章來源:http://www.zghlxwxcb.cn/news/detail-711016.html
@Test public void test(){ //ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml"); ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class); /*Demo demo = (Demo)context.getBean("demo"); System.out.println(demo);*/ DemoService demoService = (DemoService)context.getBean("demoService"); System.out.println(demoService.getDemo()); }
文章來源地址http://www.zghlxwxcb.cn/news/detail-711016.html
到了這里,關(guān)于JavaWeb-初識Spring的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!