Spring

Spring入门(使用spring框架创建对象):

1.导入Spring相关的包

junit    Spring-context    Mybatis  Mybatis-spring


2.配置文件(spring.xml) 文件中必须要有xsi官方命名空间

<bean id="接口名" class="实现类全限定名">

bean的子标签property标签(用来在类中注入属性) property中的属性:

name:属性名     ref:引用对象     value:普通值 比如double long int string

例如: <bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl">

3. 在idea中使用 Spring框架

context = new ClassPathXmlApplicationContext("文件名")

context.getBean("bean中id值")   获取接口对应的实现类对象

因为在Service中用到dao的对象 所以在spring.xml中的bean标签要使用property标签注入

例如: <bean id="accountService"class="com.itheima.service.impl.AccountServiceImpl">

                 <property  name="accountDao"  ref="accountDao"/> 

           </bean>    注入property快捷键 Alt+insert

注意: 1. 这样注入必须要求在 Service类中要有accountDao的set方法

           2. name="accountDao" 其中property标签name的属性值要和类中set方法名一致

           3.  ref="accountDao"  ref引用

二. 构造注入(使用构造函数)

格式: <constructor-arg name="字段名" value="赋值"></constructor-arg>

使用场景:使用此类时必须要带有参数 不植入参数无法创建对象


三.其他各种注入

依次为: 数组array  list 集合 set集合  map集合  properties文件 注入


四.创建对象的方式

1.实例工厂方法创建


2.Spring factoryBean 标准化方式创建


最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容