Spring_ioc入门

最近开始在学Spring,开始运用maven管理,至于maven遇到的问题,我暂时不发布在这里面,有需要的话可以百度,或者单独留言。那么,我就开始进入正题,开始总总结我这几天学习到的以及碰到的问题:
首先我们必须要明白什么是spring:
1.1Spring是一个JavaEE框架
1.2IOC Inverseduixinag of Control 反转控制的概念就是将原本程序中国手动创建UserService
对象的控制权交由Spring框架管理。
1.3Spring就是一个大工厂,可以将所有对象创建和依赖关系维护,交给Spring管理。
1.4支持AOP编程
1.5声明式事务支持,只需要通过配置,无需手动编程。
1.6方便程序测试,对Junit支持。
1.7内部提供了各种优秀框架的直接支持。
1.8降低Java EE API的使用难度
!](https://img-blog.csdnimg.cn/20190511110044983.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80Mzc4MDQ3MQ==,size_16,color_FFFFFF,t_70)
![了解Spring的运行机制](https://img-blog.csdnimg.cn/20190511105645364.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80Mzc4MDQ3MQ==,size_16,color_FFFFFF,t_70)

下面看一个简单的IOC例子
  1.创建接口
package cn.com.scitc.demo1;

public interface UserService {
public void sayHello();

}

2.创建实现类(去继承)
package cn.com.scitc.demo1;

public class UserSrviceIpml implements UserService{
public void sayHello() {
System.out.println(“success”);
}
}

3.测试类
package cn.com.scitc.demo1;

import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringDemo1 {

/**
 *传统方式
 */
@Test
public void demo1(){
    UserService userService = new UserSrviceIpml();
    userService.sayHello();
}

/*
* Spring的方式实现
* */
@Test
public void demo2(){
    //Spring工场
    ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");

    //获得工厂类
    UserService userService = (UserService) applicationContext.getBean("userService");

    //执行
    userService.sayHello();
}

}
注意:1.AppliactionConttext 获取的是.xml
2.UserService获取的是bean但是要强转
3.最后执行

spring工厂需要配置


1
Bean得实例化得三种方式
1无参

2静态工厂

3实例化工厂(在配置.xml文件得时候需要先配置工厂,后来配置类)

实例化:
1.首先创建类(举例为其中得一i个 其他都是大同小异)
public class Bean01 {
public Bean01(){
System.out.println(“Bea01被实例化成功”);
}
}
2.创建工厂类(Factraion)

public class Bean02Factory {
public static Bean02 CreateBean02(){
System.out.println(“静态工厂方法执行成功”);
return new Bean02();
}
}

3.测试
package cn.com.scitc.demo2;

import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Spring02 {
@Test
public void demo1(){
//创建工厂
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(“applicationContext.xml”);

    //获得工厂实例
    Bean01 bean01 = (Bean01) applicationContext.getBean("bean01");
    System.out.println("执行bean02 success");


}

@Test
public void demo2(){
    //创建工厂
    ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");

    //获得工厂实例
    Bean02 bean02 = (Bean02) applicationContext.getBean("bean02");
    System.out.println("==============================");
    System.out.println("执行bean02 success");
}
@Test
public void demo3(){
    //创建工厂
    ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");

    //获得工厂实例
    Bean03 bean03 = (Bean03) applicationContext.getBean("bean03");
    System.out.println("==============================");
    System.out.println("执行bean03 success");
}

}
当然这只是一个简单的例子,主要是明白spring的运行机制。

接下来便是bean的生命周期
在这里插入图片描述
在这里插入图片描述

请看demo
注意:按照步骤一定要记得配置






package cn.com.scitc.demo03;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

public class Man implements BeanNameAware, ApplicationContextAware , InitializingBean , DisposableBean {
private String name;

public String getName() {
    return name;
}

public void setName(String name) {
    System.out.println("第二步:设置属性");
    this.name = name;
}

public Man(){
    System.out.println("第一步:Man被实例化了。。。。");
}

public void setup(){
    System.out.println("第七部:Man被初始化了。。。。");
}

public void teardown(){
    System.out.println("第十一步:Man被摧毁了。。。。");
}

public void setBeanName(String s) {
    System.out.println("第三部:设置Bean的名称"+name);
}


public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
    System.out.println("第四部:了解工厂信息。。。");
}

public void afterPropertiesSet() throws Exception {
    System.out.println("第六步:属性设置后的方法。。。");
}

public void run(){
    System.out.println("第九部:执行业务处理方法。。。。");
}

public void destroy() throws Exception {
    System.out.println("第十部:执行Spring的销毁方法。。。");
}

}

后设置在这只是强调生命周期后面会有AOP
package cn.com.scitc.demo03;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;

public class MyBeanPostProcessor implements BeanPostProcessor {
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println(“第五步:初始前方法。。。。”);
return bean;
}

public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
    System.out.println("第五八步:初始后方法。。。。");
    return bean;
}

}

@Test
public void demo05(){
ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext(“applicationContext.xml”);
Man man =(Man) classPathXmlApplicationContext.getBean(“man”);

man.run();
System.out.println(man);
//十一步的摧毁
classPathXmlApplicationContext.close();

}

权限代理
1.创建类接口
2.设置增删改查的方法
3.在BeanPostProcessor里面进行权限加强
注意:返回的值是

在这里插入图片描述
如果在保存之前加强权限
1.应该先判断是否是在save方法前面
2.返回的是object类
3.return 的是proxy方法
在这里插入图片描述
4.建立测试类
在这里插入图片描述

接下来是构造方法的注入
构造方法的注入
1.创建类,类中定义对象并实例化
2.添加.xml对象(condtructory-arg)
在这里插入图片描述
3.创建测试类
在这里插入图片描述
注意:setter的方法注入需要注意两点
.xml文件里面如果有两个类,则需要用ref=“id名字”
在这里插入图片描述

如果一个类需要调用另一个类,则需要封装

p:空间的注入
1.新建类
1.1写出自己想要的 方法(set get toString)
2.建立一个子类
2.2可以被父类调用
3.配置.xml
<bean id="" class="" p:属性>

4.测试类

bean的spEL的注入(主要注意语法是在value=“#{调用需要的方法}”),其他的都和P:空间类似
!–bean的spEL的注入–>










1.新建类
1.1写出自己想要的 方法(set get toString)
2.建立一个子类
2.2可以被父类调用
3.配置.xml
<bean id="" class="" p:属性>
<property name="" value="#{调研用类.方法}"

4.测试类

复杂类型
集中数组都应该学过,就不多解释
1.创建类数组
1.1set get toString
2.配置,每个数组对应的不一样
注意:arr list 都对应的list
set对应set ==> value
map ==>


porperty ==>





hello world
hello world
hello world
hello world

<property name="list">
    <list>
        <value>1111</value>
        <value>1111</value>
        <value>1111</value>
        <value>1111</value>
    </list>
</property>

<property name="map">
    <map>
        <entry key="aa" value="11"/>
        <entry key="bb" value="22"/>
        <entry key="cc" value="33"/>
        <entry key="dd" value="44"/>
    </map>
</property>

<property name="set">
    <set>
        <value>qwqw</value>
        <value>qwqw</value>
        <value>qwqw</value>
        <value>qwqw</value>
    </set>
</property>

<property name="properties">
   <props>
       <prop key="username">admin</prop>
       <prop key="password" >123456</prop>
   </props>
</property>

在这里插入图片描述

1.创建类
1.1创建对象
在这里插入图片描述
1.2引入注解(@Service比@Compant相对要好)
在这里插入图片描述
2创建其他方法并赋值(单个方法的值用@Value=(""))

在这里插入图片描述
在这里插入图片描述

3.该类去调用其他类(或者叫做多类)
3.1创建类用@Repository
@Repository(“userDao”) //将名字改了也依然能够执行 存在弊端 所有 要引入其他属性@Quilfer和这保持一致的 名字
在这里插入图片描述

4在主类去调用
4.1先封装protive
4.2引入注解@Rocuse(=“与@Repository的地址一致”)
4.3也可以用@Autowiired + Qualifier组合
在这里插入图片描述

5.测试

@Test
public void dem3(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(“applicationContext.xml”);
UserService userService =(UserService) applicationContext.getBean(“userService”);
userService.save();
}

自动注解:
在这里插入图片描述
在这里插入图片描述

@@Component(“bean2”)//多列
@Scope(“prototype”)
public class Bean2 {

}
@Component("") 单列采用会导致路径不同 产生true
@Scope("")多列采用会导致路径不同 产生flas
在这里插入图片描述
如:在base-package指明一个包:
<context:component-scan base-package=“cn.edu.dao”/>
表明cn.gacl.java包及其子包中,如果某个类的头上带有特定的注解
@Component,@Repository,@Service,@Controller,就会将这个对象作为Bean注册进Spring容器。

在<context:component-scan base-package=” ”/>中指定多个包:

<context:component-scan base-package=“cn.edu.dao.impl,cn.edu.service.impl,cn.edu.action”/>
多个包逗号隔开。

在这里插入图片描述可以用set方法
1.建类
1.1get set
1.2将其他类引入
2.配置
<protory name=“类名” ref=调用的类id">

<bean id=“” class=""’>

3.测试

4.混用
4.1contexe:config这个标签导入
在这里插入图片描述
s4.2注销中的
在这里插入图片描述
4.3在封装类<@Rouser name="">
在这里插入图片描述
以上是ioc底层的基础,aop的将会下次列出。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值