0%

1.xml比较 区别:factory-bean class

1
2
3
4
5
6
7
<!–动态工厂–>
<bean id=”beanServiceFactory” class=”com.bean.service1.beanServiceFactory”></bean>
<bean id=”beanServiceImpl” factory-bean=”beanServiceFactory” factory-method=”add”>
</bean>

<!–静态工厂–>
<bean id=“beanServiceImpl” class=“com.bean.service1.beanServiceFactory” factory-method=“update”></bean>

2.service比较

动态service:

1
2
3
4
5
6
7
8
9
public interface beanService1 {  
public beanServiceImpl add();
}
public class beanServiceFactory implements beanService1{
//动态方法
public beanServiceImpl add() {
return new beanServiceImpl();
}
}

静态service:

1
2
3
4
5
6
public class beanServiceFactory{
//静态方法
public static beanServiceImpl update() {
return new beanServiceImpl();
}
}

3.实现类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
//动态工厂
@Test
public void test2() {
ApplicationContext applicationContext=new ClassPathXmlApplicationContext(“ApplicationContext.xml”);
beanServiceImpl beanServiceImpl= (beanServiceImpl) applicationContext.getBean(“beanServiceImpl”);
System.out.println(beanServiceImpl.getId());
}
//静态动态工厂
@Test
public void test3() {
ApplicationContext applicationContext=new ClassPathXmlApplicationContext(“ApplicationContext.xml”);
beanServiceImpl beanServiceImpl= (beanServiceImpl) applicationContext.getBean(“beanServiceImpl”);
System.out.println(beanServiceImpl.getId());
}

1.需要基本jar包

知识点:基本知识点
如何创建一个对象,通过xml配置

1
<bean id=”beanService” class=”com.bean.service.beanServiceImpl”></bean>

其中beanservice是接口,beanServiceImpl是实现类
面试题:spring容器在什么时候创建
spring容器在xml加载时,就创建对象

test测试的方法(说明了ClassPathXmlApplicationContext,FileSystemXmlApplicationContext的差别)

1
2
3
4
5
6
7
8
9
10
@Test
public void test() {
//在src的类目录下
//ApplicationContext applicationContext=new ClassPathXmlApplicationContext(“ApplicationContext.xml”);
//在项目的跟目录下 例如d:spring-demo 放在这目录下
ApplicationContext applicationContext=new FileSystemXmlApplicationContext(“ApplicationContext.xml”);
beanService beanService=(com.bean.service.beanService) applicationContext.getBean(“beanService”);
beanService.add();
beanService.update();
}

pring是什么?—面试题

spring是一个容器,管理者所有的bean的生命周期和他们之间的依赖关系。

spring的主要作用是降降低代码间的耦合度,使用不同的解耦方式。

根据功能的不同,可以将一个系统中的代码分为主业务逻辑与系统业务逻辑。

主业务逻辑有应用场景,如转账。—控制反转。ioc

系统业务逻辑 如日志,安全,事务。—–面向切面 aop

在java语言中,把数组转换成List集合,有个很方便的方法就是

1
List<String> list = Arrays.asList(“a”,”b”,”c”);

但这样得到的List它的长度是不能改变的。当你向这个List添加或删除一个元素时
例如

1
list.add(“d”);

程序就会抛出异常

1
2
3
4
java.lang.UnsupportedOperationException.
public static <T> List<T> asList(T… a) {
return new ArrayList<>(a);
}

当你看到这段代码时可能觉得没啥问题啊,不就是返回了一个ArrayList对象吗?

问题就出在这里。这个ArrayList不是java.util包下的,而是java.util.Arrays.ArrayList,显然它是Arrays类自己定义的一个内部类!

这个内部类没有实现add()、remove()方法,而是直接使用它的父类AbstractList的相应方法。

而AbstractList中的add()和remove()是直接抛出java.lang.UnsupportedOperationException异常的!

总结一下吧,如果你的List只是用来遍历,就用Arrays.asList()吧!

如果你的List还要添加或删除元素,还是乖乖地new一个java.util.ArrayList,然后一个一个的添加元素吧!

test

字符串前面和后面都是以同一个字符结束的时候,例如

1
|1|2|3|

方法1:
先截取前后两个|,然后就是1|2|3,然后就是按照普通的方法截取。

1
String[] str = userType.subSequence(1, userType.length()-1).toString().split(“\\|”);