Ioc的入门应用
1.导包
2.创建spring容器
Springioc默认的是单例模式
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<!--scope="singleton"单例
prototype 多例-->
<bean name="person" class="com.ujiuye.bean.PersonBean">
<property name="pid" value="10"/>
<property name="pname" value="张三"/>
<property name="address" value="北京"/>
<!-- ref 引用其他对象-->
<property name="card" ref="cardbean"/>
</bean>
<bean name="cardbean" class="com.ujiuye.bean.CardBean">
<property name="cid" value="10"/>
<property name="cnum" value="110"/>
</bean>
</beans>
3.测试类
@Test
public void test1(){
//1.获取spring容器
ApplicationContext app=new ClassPathXmlApplicationContext("bean.xml");
//2.获取实例
PersonBean p= (PersonBean) app.getBean("person");
PersonBean p2= (PersonBean) app.getBean("person");
System.out.println(p);
System.out.println(p==p2);
}
Name 和id区别:
Name后面可以跟多个名称 id只能有一个
name="person,p2,p3"
使用构造方法注入
<!--使用有参的构造方法di-->
<bean name="p4" class="com.ujiuye.bean.PersonBean">
<constructor-arg index="0" value="10"/>
<constructor-arg index="1" value="李四"/>
<constructor-arg index="2" value="上海"/>
</bean>
set注入不同的类型
实体类
public class PersonBean {
private int pid;
private String pname;
private String address;
private CardBean card;
private List<String> ls;
private List<CardBean> cs;
private Map map;
private Properties properties;
<bean name="person,p2,p3" class="com.ujiuye.bean.PersonBean" scope="singleton">
<property name="pid" value="10"/>
<property name="pname" value="张三"/>
<property name="address" value="北京"/>
<!-- ref 引用其他对象-->
<property name="card" ref="cardbean"/>
<property name="ls">
<list>
<value>java</value>
<value>python</value>
<value>c++</value>
</list>
</property>
<property name="cs">
<list>
<ref bean="cardbean"></ref>
<ref bean="cardbean2"></ref>
</list>
</property>
<property name="map">
<map>
<entry>
<key>
<value>name</value>
</key>
<value>admin</value>
</entry>
<entry>
<key>
<value>age</value>
</key>
<value>18</value>
</entry>
</map>
</property>
<property name="properties">
<props>
<prop key="username">root</prop>
<prop key="password">123456</prop>
</props>
</property>
</bean>
<bean name="cardbean" class="com.ujiuye.bean.CardBean">
<property name="cid" value="10"/>
<property name="cnum" value="110"/>
</bean>
<bean name="cardbean2" class="com.ujiuye.bean.CardBean">
<property name="cid" value="10"/>
<property name="cnum" value="110"/>
</bean>
Ioc对于多态的支持
1.创建dao接口 service 中使用dao
2.ServiceImpl
public class CardServiceImpl implements CardService {
private CardDao dao;
public CardDao getDao() {
return dao;
}
public void setDao(CardDao dao) {
this.dao = dao;
}
3.容器
<bean name="cardService" class="com.ujiuye.service.CardServiceImpl">
<property name="dao" ref="cardDao2"/>
</bean>
<bean name="cardDao1" class="com.ujiuye.dao.CardDaoImpl1"/>
<bean name="cardDao2" class="com.ujiuye.dao.CardDaoImpl2"/>
Dao
连接数据库进行增删改查的 sql
接口 实现类放sql
Mybatis 帮你写好了实现类
Service 业务层 可能很复杂,有可能要涉及到多张表的操作,直接到dao里面增删改查的方法即可