spring基础
maven安装
spirng-jdbc
1 2 3 4 5 6
| <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.3.21</version> </dependency>
|
xml配置文件
配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" 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-3.0.xsd">
<context:component-can base-package="cn.sdadgz"/> <context:annotation-config/>
</beans>
|
mybatis配置
替换environments和mappers
configLocation替换configuration
bean创建对象
1
| <bean id="[id]" class="[path]"></bean>
|
- id=”[id]”
- class=”[path]”
- name=”[别名]” 多个用{逗号,空格,分号}分隔(引号内部)
- scope=”[value]” prototype一人一个,默认都用一个
- autowire=”[value]” 自动装配
property定义值
1
| <property name="[name]" value="[value]"/>
|
- name=”[name]”
- value=”[value]”
- ref=”[引用的bean对象]”
null
array数组
1 2 3
| <array> <value></value> </array>
|
list
1 2 3
| <list> <value></value> </list>
|
map
1 2 3
| <map> <entry key="[key]" value="[value]"/> </map>
|
set
1 2 3
| <set> <value></value> </set>
|
Props原数据Properties
1 2 3
| <props> <prop key="[key]">[value]</prop> </props>
|
constructor-arg有参构造
1
| <constructor-arg index="[index]" value="[value]"/>
|
- index=”[index]” 从0开始
- name=”[name]”
- value=”[value]”
- ref=”[引用的bean对象]”
alias起别名
1
| <alias name="[bean对象名]" alias="[别名]"></alias>
|
import导入
1
| <import resource="[.xml文件]"/>
|
xml调用
获取ApplicationContext,spring容器
1 2
| ApplicationContext [context] = new ClassPathXmlApplicationContext("[.xml文件]");
|
操作容器
1 2
| Object [name] = [context].getBean("[name]"); [类名] [name] = [context].getBean("[name]",类名.class);
|
java代替配置文件
1 2 3 4 5 6
| @Configuration @ComponentScan(cn.sdadgz) @Import([其他配置文件].class)
@bean
|
java调用配置文件
1 2
| ApplicationContext [context] = new AnnotationConfigApplicationContext([config].class); [类名] [name] = [context].getBean("[name]");
|
注解
1 2 3 4 5 6 7 8 9 10
| @Autowired @Autowired(required = false) @Nullable @Qualifier(value = "[name]") @Resource @Component @Repository @service @Controller @Value([value])
|
动态代理
1 2 3 4 5 6 7 8
| public Object getProxy(){ return Proxy.newProxyInstance(this.getClass().getClassLoader(),[被代理的类].getClass().getInterfaces,this); }
public Object invoke(Object proxy,Method method,Object[] args)throws Throwable{ Object result = method.invoke([被代理的类],args); }
|
用到再查
SqlSessionDaoSupport
mybatis事务
c命名p命名
aop