Spring
IOCxml配置文件开发
配置导入依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.3.1</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.1</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.3.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>2.4.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot</artifactId>
<version>2.4.0</version>
</dependency>
类型注入
<bean id="stu" class="collectionType.Stu">
<property name="courses">
<array>
<value>java课程</value>
<value>数据库</value>
</array>
</property>
<property name="list">
<list>
<value>张三</value>
<value>小三</value>
</list>
</property>
<property name="maps">
<map>
<entry key="JAVA" value="java"></entry>
<entry key="PHP" value="php"></entry>
</map>
</property>
<property name="sets">
<set>
<value>MySQL</value>
<value>Redis</value>
</set>
</property>
<property name="course">
<list>
<ref bean="course1"></ref>
<ref bean="course2"></ref>
</list>
</property>
</bean>
<bean id="course1" class="collectionType.Course">
<property name="cname" value="框架"></property>
</bean>
<bean class="collectionType.Course" id="course2">
<property name="cname" value="架构"></property>
</bean>
<util:list id="booklist">
<value>易筋经</value>
<value>九阴真经</value>
</util:list>
<bean id="book" class="collectionType.Book" scope="prototype">
<property name="list" ref="booklist"></property>
</bean>
<bean id="MyBean" class="Shang15.MyBean"></bean>
public class Stu {
private String[] courses;
private List<String> list;
private Map<String,String> maps;
private Set<String> sets;
private List<Course> course;
public void setCourse(List<Course> course) {
this.course = course;
}
public void setSets(Set<String> sets) {
this.sets = sets;
}
public void setList(List<String> list) {
this.list = list;
}
public void setMaps(Map<String, String> maps) {
this.maps = maps;
}
public void setCourses(String[] courses) {
this.courses = courses;
}
public void test(){
System.out.println(Arrays.toString(courses));
System.out.println(list);
System.out.println(maps);
System.out.println(sets);
System.out.println(course);
}
}
public class Book {
private List<String> list;
public void setList(List<String> list) {
this.list = list;
}
public void test(){
System.out.println(list);
}
}
级联赋值
<bean id="userService" class="Oct29.Service.UserService">
<property name="userDao" ref="userDao"></property>
</bean>
<bean id="userDao" class="Oct29.dao.UserDaoImpl"></bean>
<bean id="emp" class="Oct29.bean.Emp">
<property name="ename" value="lucy"></property>
<property name="gender" value="女"></property>
<property name="dept">
<bean id="dept" class="Oct29.bean.Dept">
<property name="dname" value="安保"></property>
</bean>
</property>
</bean>
public class UserService {
private UserDao userDao;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
public void add() {
System.out.println("service add");
userDao.update();
}
}
public class Emp {
private String ename;
private String gender;
private Dept dept;
public Dept getDept() {
return dept;
}
public void setDept(Dept dept) {
this.dept = dept;
}
public void setEname(String ename) {
this.ename = ename;
}
public void setGender(String gender) {
this.gender = gender;
}
public void add() {
System.out.println(ename + gender + dept);
}
}
public class Dept {
private String dname;
public void setDname(String dname) {
this.dname = dname;
}
@Override
public String toString() {
return "Dept{" +
"dname='" + dname + '\'' +
'}';
}
}
配置后置管理器和自动装配
<bean id="myBeanPost" class="collectionType.MyBeanPost"></bean>
<bean id="emp" class="autowire.Emp" autowire="byType">
</bean>
<bean id="dept" class="autowire.Dept"></bean>
public class orders {
private String oname;
public orders() {
System.out.println("第一步 执行无参数构造创建bean实例");
}
public void setOname(String oname) {
this.oname = oname;
System.out.println("第二步 调用set方法设置属性值");
}
public void initMethod(){
System.out.println("第三步 执行初始化方法");
}
public void destoryMethod(){
System.out.println("第五步 执行销毁方法");
}
}
public class MyBeanPost implements BeanPostProcessor {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println("在初始化之前执行的方法");
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println("在初始化之后执行的方法");
return bean;
}
}
设置空值,特殊字符,有参构造注入属性,p名称空间注入
</bean>
<bean id="order" class="Orders">
<constructor-arg name="oname" value="abc"></constructor-arg>
<constructor-arg name="address" value="china"></constructor-arg>
</bean>
IOC注解开发
配置类代替xml配置文件
@Configuration //作为配置类,代替xml配置文件
@ComponentScan(basePackages = {"config","service","dao"})//扫描哪些包
public class SpringConfig {
}
开启组件扫描
<context:component-scan base-package="service,dao"></context:component-scan>
设置扫描内容
<context:component-scan base-package="service,dao" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<context:component-scan base-package="service,dao" >
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
实体类及测试
@Test
public void testService2(){
ApplicationContext context=new AnnotationConfigApplicationContext(config.SpringConfig.class);
UserService userService=context.getBean("userService",UserService.class);
System.out.println(userService);
userService.add();
}
@Controller
public class UserService {
@Autowired
@Qualifier(value = "userDaoImpl1")
private UserDao userDao;
public void add(){
System.out.println("service add...");
userDao.add();
}
}
AOPxml配置文件开发
反射机制
public class JDKProxy {
public static void main(String[] args) {
Class[] interfaces= {UserDao.class};
UserDaoImp1 userDao=new UserDaoImp1();
UserDao dao=(UserDao) Proxy.newProxyInstance(JDKProxy.class.getClassLoader(), interfaces,new UserDaoProxy(userDao));
int result=dao.add(1,2);
System.out.println(result);
System.out.println(dao.update("asd"));
}
}
class UserDaoProxy implements InvocationHandler{
private Object obj;
public UserDaoProxy(Object obj){
this.obj=obj;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println("方法之前执行..."+method.getName()+":传递的参数..."+ Arrays.toString(args));
Object res = method.invoke(obj,args);
System.out.println("方法之后..."+obj);
return res;
}
}
public class UserDaoImp1 implements UserDao {
@Override
public int add(int a, int b) {
System.out.println("add方法执行嘞");
return a+b;
}
@Override
public String update(String id) {
System.out.println("update方法执行嘞");
return id;
}
}
配置aopxml
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
<bean id="user" class="aopanno.User" ></bean>
<bean id="userProxy" class="aopanno.UserProxy"></bean>
<aop:config>
<aop:aspect id="aspect" ref="userProxy">
<aop:pointcut id="cut" expression="execution(* aopanno.User.add(..))"/>
<aop:after method="after" pointcut-ref="cut"></aop:after>
<aop:before method="before" pointcut-ref="cut"></aop:before>
</aop:aspect>
</aop:config>
@Component
public class User {
public void add(){
System.out.println("add....");
}
}
public class UserProxy {
@Pointcut(value = "execution(* aopanno.User.add(..))")
public void pointdemo(){
}
@Before(value = "pointdemo()")
public void before(){
System.out.println("before...");
}
@After(value = "execution(* aopanno.User.add(..))")
public void after(){
System.out.println("after...");
}
@AfterThrowing(value = "execution(* aopanno.User.add(..))")
public void afterthrowing() {
System.out.println("afterthrowing...");
}
@Around(value = "execution(* aopanno.User.add(..))")
public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
System.out.println("aroundbefore...");
proceedingJoinPoint.proceed();
System.out.println("aroundafter...");
}
@AfterReturning(value = "execution(* aopanno.User.add(..))")
public void afterReturning() {
System.out.println("afterReturning...");
}
}
AOP注解开发
开启组件扫描
<context:component-scan base-package="aopanno"></context:component-scan>
@Configuration
@ComponentScan(basePackages = {"aopanno"})
@EnableAspectJAutoProxy(proxyTargetClass = true)
public class ConfigAop {
}
@Configuration
@ComponentScan(basePackages = {"aopanno"})
public class Aspect {
}
实体类及测试
@Component
@Aspect
@Order(1)
public class UserProxy {
@Pointcut(value = "execution(* aopanno.User.add(..))")
public void pointdemo(){
}
@Before(value = "pointdemo()")
public void before(){
System.out.println("before...");
}
@After(value = "execution(* aopanno.User.add(..))")
public void after(){
System.out.println("after...");
}
@AfterThrowing(value = "execution(* aopanno.User.add(..))")
public void afterthrowing() {
System.out.println("afterthrowing...");
}
@Around(value = "execution(* aopanno.User.add(..))")
public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
System.out.println("aroundbefore...");
proceedingJoinPoint.proceed();
System.out.println("aroundafter...");
}
@AfterReturning(value = "execution(* aopanno.User.add(..))")
public void afterReturning() {
System.out.println("afterReturning...");
}
}
@Test
public void testAopanno(){
ApplicationContext context=new ClassPathXmlApplicationContext("bean.xml");
User user=context.getBean("user",User.class);
user.add();
}