Spring核心IoC

spring:

          一个为解决企业级开发复杂性而创建的开源java开发框架,他是一个分层架构(对每一层都提供了解决方法,Service:spring,controller:springmvc,dao:mybatis)。核心是Ioc,aop。

    优点:

    1、高内聚低耦合:  控制反转 (inversion of control):将所有的对象的实例化和依赖关系维护交给spring管理,spring就是一个容器。

    2、支持aop编程: spring提供对面向切面编程的支持,方便实现对程序权限的拦截,以及运行监控等功能。

    IOC的实现步骤(入门):

        导入spring相关jar包4+1:core,context,expression,bean+commons.logging

        配置xml:  <bean class="权限定名",id="一般是类名首字母小写"><bean/>

        拿到实例对象:

                    String xmlPath = "xml文件的位置";

                    ApplicationContext app = new ClassPathXmlApplicationContext(xmlPaht);

                    app.getBean("id");

    装配bean基于Xml:

                               方式一:<bean class="" id =""></bean>

                                方式二:实例工厂(所有方法必须非静态,先要有工厂再把实例工厂创建的bean交给spring。)

                                            创建一个工厂类:   public class MyBeanFactory(){

                                                                     //创建实例的方法

                                                                   public User creatUser(){return new User }

                                                           }

                                             xml配置:

                                           <bean  class ="工厂的全限定名"  id="myFactoryBean" ><bean/>

                                            <bean factory-bean="myFactoryBean" factory-meathd="creatUser" id="user"><bean/>

                                 方式三:静态工厂(所有方法必须是静态的,把静态工厂创建的bean交给spring管理。)

                                               创建一个工厂类:   public class MyBeanFactory(){

                                                                     //创建实例的方法

                                                                   public static User creatUser(){return new User }

                                                           }

                                                  xml配置:

                                                        <bean factory-meathd="creadUser" class="工厂全限定名" id="user"></bean>

            依赖注入:

                        1、构造函数

                           1.1    <bean class="" id="" >

                            <constructor-arg name ="字段名" value="注入的值"><construcor-arg/>

                                <bean/>

                            1.2   

                           <bean class="" id="" >

                            <constructor-arg  value="注入的值" type="注入属性的数据类型" index="参数的索引号,从0开始 。如果只有索引,匹配到了多个构造方法时,默认使用第一个。" ><construcor-arg/>

                                <bean/>

                        2、setter注入  

          <bean>

        <property name="uid">
<value>1</value>
</property>
<property name="name" value="小明"></property>
<property name="gender" value="男"></property>
<property name="dep" ref="depton"></property>
</bean>
        <bean id="depton" class="com.springday01_injectSetter.depton">
<property name="dname" value="开发部"></property>
<property name="usertotal" value="1"></property>
   </bean>

                        3、p命名空间 ,(需要先导入schame约束)

                        <bean id="User" class="com.springday01_injectP.User"
        p:uid="1"
        p:gender="男"
        p:name="张三"
        p:dep-ref="depton" >
        </bean>
        <bean class="com.springday01_injectP.depton" id="depton"
        p:dname="开发部"
        p:usertotal="1>
        </bean>

                        4、spEL       

                                语法:

                            <!--SpEL[了解]:对<property>进行统一编程,所有的内容都使用value
                <property name="" value="#{表达式}">
                 #{123}、#{'jack'} : 数字、字符串
            #{beanId} :另一个bean引用
            #{beanId.propName} :操作数据
            #{beanId.toString()} :执行方法
            #{T(类).字段|方法} :静态方法或字段
             -->

                    <bean id="User" class="com.springday01_SpEl.User">
<property name="uid" value="#{1}"></property>
<property name="name" value="#{'肉丝'}"></property>
<property name="gender" value="#{'女'}"></property>
<property name="dep" value="#{depton}"></property>
        </bean>
        <bean id="depton" class="com.springday01_SpEl.depton">
        <property name="dname" value="#{'营销部'}"></property>
        <property name="usertotal" value="#{1}"></property>

        </bean>

    装配bean基于注解

                         xml配置:组件扫描,扫描需要装配的bean。
                         <context:component-scan basePackge ="要扫描的范围,多个包可以用逗号隔开。"><context:component-scan/>
                        为bean配置注解:
                                     万能的:@component
                                      为了迎合三层架构的思想,我们一般。
                                                        服务层:@Service
                                                        Web层:@Controller
                                                        dao层:@Repository
                                        例如:@Repository 
                                                   public class User(){
                                                    }
                            拿到bean
                                       ApplicationContext app = new ContextPathXmlApplicationContext(xmlpath);
                                       app.getBean("user")
                                                           注意:默认是类名小写
                        依赖注入(可以注在私有字段上,也可以注入在set方法上。)
                               1、普通值@Value()
                               2、引用值
                                            2.1 按名称注入
                                                 @Autowired
                                            2.2 按类型注入
                                                      2.2.1 @AutoWired
                                                               @Qualifier()
                                                       2.2.2  @Resource()
                        声明周期有关:
                                        初始化:@PostConstruct
                                销毁:@PreDestroy
                        

                           单例,多例

                                       @Scope("prototype") 多例

        Bean种类:    

                        1、普通bean <bean id="" class><bean/>

                        2、FactoryBean 特殊bean,用于生成特定bean。

                                例如:     org.springframework.aop.framework.ProxyFactoryBean

                                ProxyFactoryBean  此工厂bean用于生成代理对象,Aop使用。

                        3、BeanFactory  用于生成任意bean。

        生命周期:init(),destory(),后处理bean BeanPostProcessor

                    xml:

    <bean id="UserServiceID" class="com.springday01_lifecycle.UserServiceImpl" init-method="myInit"                     destroy-method="myDestory" scope="singleton"></bean>

             <bean class="com.springday01_lifecycle.MyBeanPostProcessor"></bean> 

                   实体类:

   public class UserServiceImpl implements UserService {
        @Override
        public void addUser() {
    System.out.println("具体的类的方法");
                }
         public void myInit() {
    System.out.println("初始化");

        }
        public void myDestory() {注意执行条件:容器关闭,实体类是单例。
    // TODO Auto-generated method stub
    System.out.println("销毁方法");
        }

}

            后处理bean 在初始化方法前后执行。

            public class MyBeanPostProcessor implements BeanPostProcessor{
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName)
throws BeansException {
System.out.println(beanName+"在初始化化方法前执行");
return bean;
}
@Override
public Object postProcessAfterInitialization(final Object bean, String beanName)
throws BeansException {
System.out.println(beanName+"在初始化方法后执行,");
//目标对象Bean,在指定方法的前后执行其他操作(事务的管理).
//生成jdk代理
return Proxy.newProxyInstance(
MyBeanPostProcessor.class.getClassLoader(),
bean.getClass().getInterfaces(), 
new InvocationHandler(){
@Override
public Object invoke(Object arg0, Method arg1, Object[] arg2)
throws Throwable {
System.out.println("开启事务");
Object obj = arg1.invoke(bean, arg2);
System.out.println("提交事务");
return obj;

}
});

}

             API :
                    String xmlPath = "com/springday01_api/mySpringAPI.xml";
                   1、BeanFactory 采用延迟加载,第一次getBean()才被实列化。是最大的父接口。
BeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource(xmlPath));
beanFactory.getBean("user");
                    2、applicationContext 加载配置文件是就被是例化。Beanfacotry的子接口,功能更强大。
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);

                                beanFactory.getBean("user");

              配置文件的加载:
                    1、classPathXmlApplicationContext 加载
                        用于加载classpath(类路径、src)下的xml
                        加载xml运行时位置 --> /WEB-INF/classes/...xml
                         new classPathXmlApplicationContext(xmlPath)

                    2、FileSystemXmlApplicationContext
                                 加载指定盘符的配置文件
                                  加载xml运行时位置 --> /WEB-INF/...xml
                        new FileSystemXmlApplicationContext(xmlPath)
                

                

                

                        

                        

        

                            

                            

                                       
                                            
                                                            
                                            
                                            
                                  

                                            

       

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值