--spring框架
--spring基本配置
--新建目錄src/main/resources
--在此目錄新建文件applicationContext.xml文件內容如下:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
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
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
">
</beans>
--三個文件的下載可以通過相應網址下載http://www.springframework.org/schema/beans/spring-beans.xsd
--添加代碼提示
window->prefenerces->搜索xml->xml Catalog->User Specified..->add->Location->File System..->找到xsd文件->key type改成Schema location->key改成ttp://www.springframework.org/schema/beans/spring-beans.xsd對應網址
--老版本的配置方式
--創建對一個類的管理
在xml配置文件中添加<bean id="person" class="com.root.springdemo.beans.Person"></bean>類似的語句
--新建的其他包使用配置文件的方法
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");//配置文件名稱
Person p = (Person) context.getBean("person");//id
--直接在配置文件中對類的屬性賦值
<bean id="person" class="com.root.springdemo.beans.Person">
<property name="id" value="120"></property>
</bean>
--新版本的配置方式-註解
方法是在java文件上添加@Commponent("名稱")
引入包import org.springframework.stereotype.Component;
--面向切面技術
@Component
@Aspect
//如何在一個方法或者有一個包中所有方法都執行的函數
//execution 表達式(返回值類型,包名.類名.方法名(參數)),返回值類型不寫可以用*代替
@Before("execution(void com.root.springdemo.aopdemo.PopStar.sing())")
//@Before("execution(void com.root.springdemo.aopdemo.*.*(..))")
//意思是該包下的所有類和方法,..是不考慮參數
--打開aop aspectj-在applicationContext.xml配置
<aop:aspectj-autoproxy proxy-target-class="true"></aop:aspectj-autoproxy>
--別忘了spring先關jar包配置
<!-- spring context-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.8.RELEASE</version>
</dependency>
<!-- spring core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.0.8.RELEASE</version>
</dependency>
<!-- spring beans -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.0.8.RELEASE</version>
</dependency>
<!-- aspectjweaver -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.1</version>
</dependency>
注意前三個版本一定要版本一致