在本文中,我们将为您详细介绍java.beans.beancontext.BeanContextServices的实例源码的相关知识,并且为您解答关于java.beans.introspection的
在本文中,我们将为您详细介绍java.beans.beancontext.BeanContextServices的实例源码的相关知识,并且为您解答关于java.beans.introspection的疑问,此外,我们还会提供一些关于android.view.textservice.SentenceSuggestionsInfo的实例源码、android.view.textservice.TextServicesManager的实例源码、ApplicationContext注入Bean(多线程中注入Bean)、bean实例化的三种方式 bean标签常用属性 单例模式和多例模式的对象 BeanFactory和ApplicationContext:的有用信息。
本文目录一览:- java.beans.beancontext.BeanContextServices的实例源码(java.beans.introspection)
- android.view.textservice.SentenceSuggestionsInfo的实例源码
- android.view.textservice.TextServicesManager的实例源码
- ApplicationContext注入Bean(多线程中注入Bean)
- bean实例化的三种方式 bean标签常用属性 单例模式和多例模式的对象 BeanFactory和ApplicationContext:
java.beans.beancontext.BeanContextServices的实例源码(java.beans.introspection)
/** * @param bcSAE the BeanContextServiceAvailableEvent */ public void serviceAvailable(BeanContextServiceAvailableEvent bcSAE) { // Get a reference to the context BeanContextServices context = bcSAE.getSourceAsBeanContextServices(); // Use the service,if it's available if (context.hasService(TestCounter.class)) { // System.out.println("Attempting to use the service..."); try { // Got the service service = (TestCounter) context.getService(this,this,TestCounter.class,str,this); service.counter(); } catch (Exception e) { } this.bcSAE = bcSAE; } }
public static void main(String[] args) { for (int i = 0; i < 10; i++) { BeanContextServices container = new BeanContextServicesSupport(); BeanContextChild ms1 = new MyService1(); BeanContextServices ms2 = new MyService2(); BeanContextChild mb = new MyBean(); container.add(ms1); container.add(ms2); ms2.add(mb); // exception thrown here container.remove(ms2); } }
public static void main(String[] args) { for (int i = 0; i < 10; i++) { BeanContextServices container = new BeanContextServicesSupport(); BeanContextChild ms1 = new MyService1(); BeanContextServices ms2 = new MyService2(); BeanContextChild mb = new MyBean(); container.add(ms1); container.add(ms2); ms2.add(mb); // exception thrown here container.remove(ms2); } }
public static void main(String[] args) { for (int i = 0; i < 10; i++) { BeanContextServices container = new BeanContextServicesSupport(); BeanContextChild ms1 = new MyService1(); BeanContextServices ms2 = new MyService2(); BeanContextChild mb = new MyBean(); container.add(ms1); container.add(ms2); ms2.add(mb); // exception thrown here container.remove(ms2); } }
public static void main(String[] args) { for (int i = 0; i < 10; i++) { BeanContextServices container = new BeanContextServicesSupport(); BeanContextChild ms1 = new MyService1(); BeanContextServices ms2 = new MyService2(); BeanContextChild mb = new MyBean(); container.add(ms1); container.add(ms2); ms2.add(mb); // exception thrown here container.remove(ms2); } }
public static void main(String[] args) { for (int i = 0; i < 10; i++) { BeanContextServices container = new BeanContextServicesSupport(); BeanContextChild ms1 = new MyService1(); BeanContextServices ms2 = new MyService2(); BeanContextChild mb = new MyBean(); container.add(ms1); container.add(ms2); ms2.add(mb); // exception thrown here container.remove(ms2); } }
public static void main(String[] args) { for (int i = 0; i < 10; i++) { BeanContextServices container = new BeanContextServicesSupport(); BeanContextChild ms1 = new MyService1(); BeanContextServices ms2 = new MyService2(); BeanContextChild mb = new MyBean(); container.add(ms1); container.add(ms2); ms2.add(mb); // exception thrown here container.remove(ms2); } }
public static void main(String[] args) { for (int i = 0; i < 10; i++) { BeanContextServices container = new BeanContextServicesSupport(); BeanContextChild ms1 = new MyService1(); BeanContextServices ms2 = new MyService2(); BeanContextChild mb = new MyBean(); container.add(ms1); container.add(ms2); ms2.add(mb); // exception thrown here container.remove(ms2); } }
public static void main(String[] args) { for (int i = 0; i < 10; i++) { BeanContextServices container = new BeanContextServicesSupport(); BeanContextChild ms1 = new MyService1(); BeanContextServices ms2 = new MyService2(); BeanContextChild mb = new MyBean(); container.add(ms1); container.add(ms2); ms2.add(mb); // exception thrown here container.remove(ms2); } }
public static void main(String[] args) { for (int i = 0; i < 10; i++) { BeanContextServices container = new BeanContextServicesSupport(); BeanContextChild ms1 = new MyService1(); BeanContextServices ms2 = new MyService2(); BeanContextChild mb = new MyBean(); container.add(ms1); container.add(ms2); ms2.add(mb); // exception thrown here container.remove(ms2); } }
public static void main(String[] args) { for (int i = 0; i < 10; i++) { BeanContextServices container = new BeanContextServicesSupport(); BeanContextChild ms1 = new MyService1(); BeanContextServices ms2 = new MyService2(); BeanContextChild mb = new MyBean(); container.add(ms1); container.add(ms2); ms2.add(mb); // exception thrown here container.remove(ms2); } }
public void testBeanContextServiceRevokedEvent() { BeanContextServices services = new MockBeanContextServices(); BeanContextServiceRevokedEvent event = new MockBeanContextServiceRevokedEvent( services,BeanContext.class,true); assertSame(BeanContext.class,event.getServiceClass()); assertSame(services,event.getSource()); assertSame(services,event.getSourceAsBeanContextServices()); assertTrue(event.isCurrentServiceInvalidNow()); }
public void testIsServiceClass() { BeanContextServices services = new MockBeanContextServices(); BeanContextServiceRevokedEvent event = new MockBeanContextServiceRevokedEvent( services,true); assertTrue(event.isServiceClass(BeanContext.class)); assertFalse(event.isServiceClass(Integer.class)); // Regression for HARMONY-1516 assertFalse(event.isServiceClass(null)); }
public void testIsCurrentServiceInvalidNow() { BeanContextServices services = new MockBeanContextServices(); BeanContextServiceRevokedEvent event = new MockBeanContextServiceRevokedEvent( services,true); assertTrue(event.isCurrentServiceInvalidNow()); event = new MockBeanContextServiceRevokedEvent(services,false); assertFalse(event.isCurrentServiceInvalidNow()); }
public void testConstructor() throws Exception { BeanContextServices bcs = new MockBeanContextServices(); BeanContextServiceRevokedEvent event = new BeanContextServiceRevokedEvent( bcs,ArrayList.class,true); assertEquals(null,event.getPropagatedFrom()); assertEquals(ArrayList.class,event.getServiceClass()); assertSame(bcs,event.getSource()); assertSame(bcs,event.getBeanContext()); assertSame(bcs,event.getSourceAsBeanContextServices()); assertFalse(event.isPropagated()); }
public void testBeanContextServiceAvailableEvent_Nullparam() { BeanContextServices services = new MockBeanContextServices(); try { new MockBeanContextServiceAvailableEvent(null,BeanContext.class); fail("IAE expected"); } catch (IllegalArgumentException e) { // expected } BeanContextServiceAvailableEvent event = new MockBeanContextServiceAvailableEvent( services,null); assertNull(event.getServiceClass()); assertSame(services,event.getSourceAsBeanContextServices()); }
public void testBeanContextServiceAvailableEvent() { BeanContextServices services = new MockBeanContextServices(); BeanContextServiceAvailableEvent event = new MockBeanContextServiceAvailableEvent( services,BeanContext.class); assertSame(BeanContext.class,event.getSourceAsBeanContextServices()); }
public void testGetSourceAsBeanContextServices() { BeanContextServices services = new MockBeanContextServices(); BeanContextServiceAvailableEvent event = new MockBeanContextServiceAvailableEvent( services,BeanContext.class); assertSame(services,event.getSourceAsBeanContextServices()); }
public void testGetCurrentServiceSelectors() { BeanContextServices services = new MockBeanContextServices(); BeanContextServiceAvailableEvent event = new MockBeanContextServiceAvailableEvent( services,BeanContext.class); Iterator expectedIt = services .getCurrentServiceSelectors(BeanContext.class); Iterator it = event.getCurrentServiceSelectors(); while (expectedIt.hasNext()) { assertSame(expectedIt.next(),it.next()); } assertFalse(expectedIt.hasNext()); assertFalse(it.hasNext()); }
public Object getService(BeanContextServices bcs,Object requestor,Class serviceClass,Object serviceSelector) { Object result = Collections.EMPTY_SET; records.add("getService",bcs,requestor,serviceClass,serviceSelector,result); return result; }
public static void main(String[] args) { for (int i = 0; i < 10; i++) { BeanContextServices container = new BeanContextServicesSupport(); BeanContextChild ms1 = new MyService1(); BeanContextServices ms2 = new MyService2(); BeanContextChild mb = new MyBean(); container.add(ms1); container.add(ms2); ms2.add(mb); // exception thrown here container.remove(ms2); } }
public static void main(String[] args) { for (int i = 0; i < 10; i++) { BeanContextServices container = new BeanContextServicesSupport(); BeanContextChild ms1 = new MyService1(); BeanContextServices ms2 = new MyService2(); BeanContextChild mb = new MyBean(); container.add(ms1); container.add(ms2); ms2.add(mb); // exception thrown here container.remove(ms2); } }
public void testBeanContextServiceRevokedEvent() { BeanContextServices services = new MockBeanContextServices(); BeanContextServiceRevokedEvent event = new MockBeanContextServiceRevokedEvent( services,event.getSourceAsBeanContextServices()); assertTrue(event.isCurrentServiceInvalidNow()); }
public void testIsServiceClass() { BeanContextServices services = new MockBeanContextServices(); BeanContextServiceRevokedEvent event = new MockBeanContextServiceRevokedEvent( services,true); assertTrue(event.isServiceClass(BeanContext.class)); assertFalse(event.isServiceClass(Integer.class)); // Regression for HARMONY-1516 assertFalse(event.isServiceClass(null)); }
public void testIsCurrentServiceInvalidNow() { BeanContextServices services = new MockBeanContextServices(); BeanContextServiceRevokedEvent event = new MockBeanContextServiceRevokedEvent( services,false); assertFalse(event.isCurrentServiceInvalidNow()); }
public void testBeanContextServiceAvailableEvent_Nullparam() { BeanContextServices services = new MockBeanContextServices(); try { new MockBeanContextServiceAvailableEvent(null,event.getSourceAsBeanContextServices()); }
public void testBeanContextServiceAvailableEvent() { BeanContextServices services = new MockBeanContextServices(); BeanContextServiceAvailableEvent event = new MockBeanContextServiceAvailableEvent( services,event.getSourceAsBeanContextServices()); }
public void testGetSourceAsBeanContextServices() { BeanContextServices services = new MockBeanContextServices(); BeanContextServiceAvailableEvent event = new MockBeanContextServiceAvailableEvent( services,event.getSourceAsBeanContextServices()); }
public void testGetCurrentServiceSelectors() { BeanContextServices services = new MockBeanContextServices(); BeanContextServiceAvailableEvent event = new MockBeanContextServiceAvailableEvent( services,it.next()); } assertFalse(expectedIt.hasNext()); assertFalse(it.hasNext()); }
public Object getService(BeanContextServices bcs,result); return result; }
public void testConstructor() throws Exception { BeanContextServices bcs = new MockBeanContextServices(); BeanContextServiceRevokedEvent event = new BeanContextServiceRevokedEvent( bcs,event.getSourceAsBeanContextServices()); assertFalse(event.isPropagated()); }
public Object getService(BeanContextServices bcs,Object serviceSelector) { final String str = (String)serviceSelector; return new TestCounter() { public void counter() { String res = str; System.out.println(str); } }; }
android.view.textservice.SentenceSuggestionsInfo的实例源码
@Override public void onGetSentenceSuggestions(SentenceSuggestionsInfo[] results) { try { final List<String> sb = new ArrayList<>(); for (int i = 0; i < results.length; ++i) { final SentenceSuggestionsInfo ssi = results[i]; for (int j = 0; j < ssi.getSuggestionsCount(); ++j) { dumpSuggestionsInfoInternal( sb,ssi.getSuggestionsInfoAt(j),ssi.getoffsetAt(j),ssi.getLengthAt(j)); } } setSuggestions(sb,true,true); } catch(Exception e){} }
/** * Callback for {@link SpellCheckerSession#getSentenceSuggestions(TextInfo[],int)} * @param results an array of {@link SentenceSuggestionsInfo}s. * These results are suggestions for {@link TextInfo}s * queried by {@link SpellCheckerSession#getSentenceSuggestions(TextInfo[],int)}. */ @Override public void onGetSentenceSuggestions(final SentenceSuggestionsInfo[] arg0) { if (!isSentenceSpellCheckSupported()) { Log.e(TAG,"Sentence spell check is not supported on this platform," + "but accidentially called."); return; } Log.d(TAG,"onGetSentenceSuggestions"); final StringBuilder sb = new StringBuilder(); for (int i = 0; i < arg0.length; ++i) { final SentenceSuggestionsInfo ssi = arg0[i]; for (int j = 0; j < ssi.getSuggestionsCount(); ++j) { dumpSuggestionsInfoInternal( sb,ssi.getLengthAt(j)); } } runOnUiThread(new Runnable() { @Override public void run() { mMainView.append(sb.toString()); } }); }
@Override public SentenceSuggestionsInfo[] onGetSentenceSuggestionsMultiple(TextInfo[] textInfos,int suggestionsLimit) { final SentenceSuggestionsInfo[] retval = splitAndSuggest(textInfos,suggestionsLimit); if (retval == null || retval.length != textInfos.length) { return retval; } for (int i = 0; i < retval.length; ++i) { final SentenceSuggestionsInfo tempSsi = fixWronglyInvalidatedWordWithSingleQuote(textInfos[i],retval[i]); if (tempSsi != null) { retval[i] = tempSsi; } } return retval; }
/** * Checks for typos and sends results back to native through a JNI call. * @param results Results returned by the Android spellchecker. */ @Override public void onGetSentenceSuggestions(SentenceSuggestionsInfo[] results) { ArrayList<Integer> offsets = new ArrayList<Integer>(); ArrayList<Integer> lengths = new ArrayList<Integer>(); for (SentenceSuggestionsInfo result : results) { for (int i = 0; i < result.getSuggestionsCount(); i++) { // If a word looks like a typo,record its offset and length. if ((result.getSuggestionsInfoAt(i).getSuggestionsAttributes() & SuggestionsInfo.RESULT_ATTR_LOOKS_LIKE_TYPO) == SuggestionsInfo.RESULT_ATTR_LOOKS_LIKE_TYPO) { offsets.add(result.getoffsetAt(i)); lengths.add(result.getLengthAt(i)); } } } nativeProcessspellCheckResults(mNativeSpellCheckerSessionBridge,convertListToArray(offsets),convertListToArray(lengths)); }
@Override public SentenceSuggestionsInfo[] onGetSentenceSuggestionsMultiple(TextInfo[] textInfos,int suggestionsLimit) { final SentenceSuggestionsInfo[] retval = super.onGetSentenceSuggestionsMultiple(textInfos,retval[i]); if (tempSsi != null) { retval[i] = tempSsi; } } return retval; }
@TargetApi(Build.VERSION_CODES.JELLY_BEAN) public static SentenceSuggestionsInfo reconstructSuggestions( SentenceTextInfoParams originalTextInfoParams,SuggestionsInfo[] results) { if (results == null || results.length == 0) { return null; } if (originalTextInfoParams == null) { return null; } final int originalCookie = originalTextInfoParams.mOriginalTextInfo.getCookie(); final int originalSequence = originalTextInfoParams.mOriginalTextInfo.getSequence(); final int querySize = originalTextInfoParams.mSize; final int[] offsets = new int[querySize]; final int[] lengths = new int[querySize]; final SuggestionsInfo[] reconstructedSuggestions = new SuggestionsInfo[querySize]; for (int i = 0; i < querySize; ++i) { final SentenceWordItem item = originalTextInfoParams.mItems.get(i); SuggestionsInfo result = null; for (int j = 0; j < results.length; ++j) { final SuggestionsInfo cur = results[j]; if (cur != null && cur.getSequence() == item.mTextInfo.getSequence()) { result = cur; result.setCookieAndSequence(originalCookie,originalSequence); break; } } offsets[i] = item.mStart; lengths[i] = item.mLength; reconstructedSuggestions[i] = result != null ? result : EMPTY_SUGGESTIONS_INFO; } return new SentenceSuggestionsInfo(reconstructedSuggestions,offsets,lengths); }
/** * Get sentence suggestions for specified texts in an array of TextInfo. This is taken from * SpellCheckerService#onGetSentenceSuggestionsMultiple that we can't use because it's * using private variables. * The default implementation splits the input text to words and returns * {@link SentenceSuggestionsInfo} which contains suggestions for each word. * This function will run on the incoming IPC thread. * So,this is not called on the main thread,* but will be called in series on another thread. * @param textInfos an array of the text Metadata * @param suggestionsLimit the maximum number of suggestions to be returned * @return an array of {@link SentenceSuggestionsInfo} returned by * {@link android.service.textservice.SpellCheckerService.Session#onGetSuggestions(TextInfo,int)} */ private SentenceSuggestionsInfo[] splitAndSuggest(TextInfo[] textInfos,int suggestionsLimit) { if (textInfos == null || textInfos.length == 0) { return SentenceLevelAdapter.getEmptySentenceSuggestionsInfo(); } SentenceLevelAdapter sentenceLevelAdapter; synchronized(this) { sentenceLevelAdapter = mSentenceLevelAdapter; if (sentenceLevelAdapter == null) { final String localeStr = getLocale(); if (!TextUtils.isEmpty(localeStr)) { sentenceLevelAdapter = new SentenceLevelAdapter(mResources,new Locale(localeStr)); mSentenceLevelAdapter = sentenceLevelAdapter; } } } if (sentenceLevelAdapter == null) { return SentenceLevelAdapter.getEmptySentenceSuggestionsInfo(); } final int infosSize = textInfos.length; final SentenceSuggestionsInfo[] retval = new SentenceSuggestionsInfo[infosSize]; for (int i = 0; i < infosSize; ++i) { final SentenceLevelAdapter.SentenceTextInfoParams textInfoParams = sentenceLevelAdapter.getSplitWords(textInfos[i]); final ArrayList<SentenceLevelAdapter.SentenceWordItem> mItems = textInfoParams.mItems; final int itemsSize = mItems.size(); final TextInfo[] splitTextInfos = new TextInfo[itemsSize]; for (int j = 0; j < itemsSize; ++j) { splitTextInfos[j] = mItems.get(j).mTextInfo; } retval[i] = SentenceLevelAdapter.reconstructSuggestions( textInfoParams,onGetSuggestionsMultiple( splitTextInfos,suggestionsLimit,true)); } return retval; }
/** * Checks for typos and sends results back to native through a JNI call. * @param results Results returned by the Android spellchecker. */ @Override public void onGetSentenceSuggestions(SentenceSuggestionsInfo[] results) { mStopMs = SystemClock.elapsedRealtime(); if (mNativeSpellCheckerSessionBridge == 0) { return; } ArrayList<Integer> offsets = new ArrayList<Integer>(); ArrayList<Integer> lengths = new ArrayList<Integer>(); for (SentenceSuggestionsInfo result : results) { if (result == null) { // In some cases null can be returned by the selected spellchecking service,// see crbug.com/651458. In this case skip to next result to avoid a // NullPointerException later on. continue; } for (int i = 0; i < result.getSuggestionsCount(); i++) { // If a word looks like a typo,record its offset and length. if ((result.getSuggestionsInfoAt(i).getSuggestionsAttributes() & SuggestionsInfo.RESULT_ATTR_LOOKS_LIKE_TYPO) == SuggestionsInfo.RESULT_ATTR_LOOKS_LIKE_TYPO) { offsets.add(result.getoffsetAt(i)); lengths.add(result.getLengthAt(i)); } } } nativeProcessspellCheckResults(mNativeSpellCheckerSessionBridge,convertListToArray(lengths)); RecordHistogram.recordTimesHistogram("SpellCheck.Android.Latency",mStopMs - mStartMs,TimeUnit.MILLISECONDS); }
public static SentenceSuggestionsInfo[] getEmptySentenceSuggestionsInfo() { return EmptySentenceSuggestionsInfosInitializationHolder.EMPTY_SENTENCE_SUGGESTIONS_INFOS; }
@Override public void onGetSentenceSuggestions(SentenceSuggestionsInfo[] results) { }
android.view.textservice.TextServicesManager的实例源码
/** * Constructs a SpellCheckerSessionBridge object as well as its SpellCheckerSession object. * @param nativeSpellCheckerSessionBridge Pointer to the native SpellCheckerSessionBridge. */ private SpellCheckerSessionBridge(long nativeSpellCheckerSessionBridge) { mNativeSpellCheckerSessionBridge = nativeSpellCheckerSessionBridge; Context context = ApplicationStatus.getApplicationContext(); final TextServicesManager textServicesManager = (TextServicesManager) context.getSystemService( Context.TEXT_SERVICES_MANAGER_SERVICE); // This combination of parameters will cause the spellchecker to be based off of // the language specified at "Settings > Language & input > Spell checker > Language". // If that setting is set to "Use system language" and the system language is not on the // list of supported spellcheck languages,this call will return null. This call will also // return null if the user has turned spellchecking off at "Settings > Language & input > // Spell checker". mSpellCheckerSession = textServicesManager.newSpellCheckerSession(null,null,this,true); }
/** * Constructs a SpellCheckerSessionBridge object as well as its SpellCheckerSession object. * @param nativeSpellCheckerSessionBridge Pointer to the native SpellCheckerSessionBridge. */ private SpellCheckerSessionBridge(long nativeSpellCheckerSessionBridge) { mNativeSpellCheckerSessionBridge = nativeSpellCheckerSessionBridge; Context context = ContextUtils.getApplicationContext(); final TextServicesManager textServicesManager = (TextServicesManager) context.getSystemService( Context.TEXT_SERVICES_MANAGER_SERVICE); // This combination of parameters will cause the spellchecker to be based off of // the language specified at "Settings > Language & input > Spell checker > Language". // If that setting is set to "Use system language" and the system language is not on the // list of supported spellcheck languages,true); }
/** * Main initialization of the input method component. Be sure to call * to super class. */ @Override public void onCreate() { super.onCreate(); mInputMethodManager = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE); mWordSeparators = getResources().getString(R.string.word_separators); final TextServicesManager tsm = (TextServicesManager) getSystemService( Context.TEXT_SERVICES_MANAGER_SERVICE); mScs = tsm.newSpellCheckerSession(null,true); }
@Override public void onResume() { super.onResume(); final TextServicesManager tsm = (TextServicesManager) getSystemService( Context.TEXT_SERVICES_MANAGER_SERVICE); mScs = tsm.newSpellCheckerSession(null,true); if (mScs != null) { // Instantiate TextInfo for each query // TextInfo can be passed a sequence number and a cookie number to identify the result if (isSentenceSpellCheckSupported()) { // Note that getSentenceSuggestions works on JB or later. Log.d(TAG,"Sentence spellchecking supported."); mScs.getSentenceSuggestions(new TextInfo[] {new TextInfo("tgisis")},3); mScs.getSentenceSuggestions(new TextInfo[] {new TextInfo( "I wold like to here form you")},3); mScs.getSentenceSuggestions(new TextInfo[] {new TextInfo("hell othere")},3); } else { // Note that getSuggestions() is a deprecated API. // It is recommended for an application running on Jelly Bean or later // to call getSentenceSuggestions() only. mScs.getSuggestions(new TextInfo("tgis"),3); mScs.getSuggestions(new TextInfo("hllo"),3); mScs.getSuggestions(new TextInfo("helloworld"),3); } } else { Log.e(TAG,"Couldn't obtain the spell checker service."); } }
private void updateTextServicesLocaleLocked() { final TextServicesManager textServicesManager = (TextServicesManager) mContext.getSystemService(Context.TEXT_SERVICES_MANAGER_SERVICE); final SpellCheckerSubtype subtype = textServicesManager.getCurrentSpellCheckerSubtype(true); final Locale locale; if (subtype != null) { locale = SpellCheckerSubtype.constructLocaleFromString(subtype.getLocale()); } else { locale = null; } mCurrentSpellCheckerLocaleCache = locale; }
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.spellchecker); check = (Button) findViewById(R.id.checkerbuttoncheck); checktext = (EditText) findViewById(R.id.checkeredittext); suggestBox = (TextView) findViewById(R.id.checkersuggestview); rightBox = (TextView) findViewById(R.id.righttext); preferences = this.getSharedPreferences("team7663.project.camword",Context.MODE_PRIVATE); preferences.edit().putBoolean("team7663.project.camword.OCR.mainoff",true).apply(); checktext.setText(preferences.getString("team7663.project.camword.OCR.savespell","")); TextServicesManager tsm = (TextServicesManager) getSystemService(Context.TEXT_SERVICES_MANAGER_SERVICE); scs = tsm.newSpellCheckerSession(null,SpellChecker.this,true); MainActivity.spell.setBackgroundResource(R.drawable.spell); check.setonClickListener(new View.OnClickListener() { @SuppressWarnings("deprecation") @Override public void onClick(View v){ value = checktext.getText().toString(); rightBox.setText(""); if(value.isEmpty()==false) { scs.getSuggestions(new TextInfo(value),5); } } }); }
/** * 返回 {@link TextServicesManager} */ public static TextServicesManager getTextServicesManager() { return (TextServicesManager) get(TEXT_SERVICES_MANAGER_SERVICE); }
@TargetApi(14) public static TextServicesManager getTextServicesManager() { return (TextServicesManager) getSystemService(Context.TEXT_SERVICES_MANAGER_SERVICE); }
public static TextServicesManager textServices(Context context) { return (TextServicesManager) context.getSystemService(TEXT_SERVICES_MANAGER_SERVICE); }
public static TextServicesManager textServicesManager() { return (TextServicesManager) get(Context.TEXT_SERVICES_MANAGER_SERVICE); }
/** * Obtain a {@link TextServicesManager} instance associated with specified {@link Context} * * @param context Context * @return {@link TextServicesManager} associated with specified {@link Context} * @throws InvalidContextException if {@link TextServicesManager} can't be obtained * from specified {@link Context} */ @NonNull public static TextServicesManager getTextServicesManager(@NonNull Context context) { return validate(context.getSystemService(Context.TEXT_SERVICES_MANAGER_SERVICE)); }
ApplicationContext注入Bean(多线程中注入Bean)
文章目录
- 前言
- 一、线程中注入Service层或Dao层
- 总结
前言
通常我们用一下几种方式注入 :
1、
@Autowired
是通过 byType 的方式去注入的, 使用该注解,要求接口只能有一个实现类。
2、@Resource
可以通过 byName 和 byType的方式注入, 默认先按 byName的方式进行匹配,如果匹配不到,再按 byType的方式进行匹配。
3、@Qualifier
注解可以按名称注入, 但是注意是 类名。
一、线程中注入Service层或Dao层
有些情况我们需要在工具类或在new一个线程之后,线程中注入Service层或Dao层,
这时候用以上方法是注入不进去
用以下方法即可注入:
package com.java.base.config;
import java.lang.annotation.Annotation;
import java.util.Map;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;
/***
*
* <p>Title: MyApplicationContext</p>
* <p>Description: ApplicationContextAware 通过它spring容器会自动把上下文环境对象调用ApplicationContextAware接口中的setApplicationContext方法。
* 通过这个上下文环境对象得到spring容器中的Bean
* 在我们写的工具类获取线程中不能直接通过Spring注入,这个时候就需要通过ApplicationContext获取Bean
* </p>
* @author shy
*/
@Service
public class MyApplicationContext implements ApplicationContextAware {
private static ApplicationContext context;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
context = applicationContext;
}
public static <T> T getBean(final Class<T> requiredType) {
return context.getBean(requiredType);
}
public static <T> T getBean(final String beanName) {
@SuppressWarnings("unchecked")
final T bean = (T) context.getBean(beanName);
return bean;
}
public static <T> Map<String, T> getBeans(final Class<T> requiredType) {
return context.getBeansOfType(requiredType);
}
public static Map<String, Object> getBeansWithAnnotation(final Class<? extends Annotation> annotationType) {
return context.getBeansWithAnnotation(annotationType);
}
}
private XXXrService xxxService = MyApplicationContext.getBean(XXXService.class);
总结
如果此篇文章有帮助到您, 希望打大佬们能
关注
、点赞
、收藏
、评论
支持一波,非常感谢大家!
如果有不对的地方请指正!!!
参考1
bean实例化的三种方式 bean标签常用属性 单例模式和多例模式的对象 BeanFactory和ApplicationContext:
bean实例化的三种方式 bean标签常用属性 单例模式和多例模式的对象 beanfactory和ApplicationContext:
- 学习网站/博客:
- 1.bean实例化的三种方式:
- 页面结构整体布局:
- 运行结果如下:
- 代码如下:
- pom.xml:
- Student:
- MyStaticFactory :
- MyInstanceFactory :
- Test:
- spring-1.xml:
- 2.bean标签常用属性:
- 代码整体布局:
- 运行结果:
- 添加/修改的代码如下:
- spring-2.xml:(添加)
- Student:(修改)
- (test)Test2:
- 3.单例模式和多例模式的对象:
- 运行结果:
- God:
- Test:
- (singleton)God:(添加)
- (singleton)Test:(添加)
- 4.BeanFactory和ApplicationContext:
学习网站/博客:
spring https://spring.io/
The loC Container https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#beans
Spring官方文档(中文版!!!) https://blog.csdn.net/li1376417539/article/details/104951358/
1.bean实例化的三种方式:
页面结构整体布局:
运行结果如下:
代码如下:
pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>lesson0820_spring</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<!--引入spring-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.18</version>
</dependency>
</dependencies>
</project>
Student:
package com.entity;
public class Student {
public Student(){
System.out.println("这是Student的构造方法...");
}
public void introduce(){
System.out.println("我是一个学生");
}
}
MyStaticFactory :
package com.factory;
import com.entity.Student;
/**
* 静态工厂
*/
public class MyStaticFactory {
/**
* 静态返回学生实例
* @return
*/
public static Student createInstance(){
System.out.println("调用静态工厂.....MyStaticFactory...createInstance");
return new Student();
}
}
MyInstanceFactory :
package com.factory;
import com.entity.Student;
/**
* 实例工厂
*/
public class MyInstanceFactory {
/**
* 定义创建对象的实例方法
* @return
*/
public Student createInstance(){
return new Student();
}
}
Test:
package com.test;
import com.entity.Student;
import com.factory.MyInstanceFactory;
import com.factory.MyStaticFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClasspathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
//Student student = new Student();
//创建spring bean容器对象
ApplicationContext ac = new ClasspathXmlApplicationContext("spring-1.xml");
// Student student = ac.getBean(Student.class);
//静态工厂创建的对象,通过id名字,获取bean对象
// Student s = (Student) ac.getBean("stu");
// s.introduce();
//实例工厂方式创建对象
Student s2 = (Student) ac.getBean("stu2");
s2.introduce();
}
}
spring-1.xml:
<?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.xsd"
default-lazy-init="true"
>
<!--class属性必须配置,用于指明要创建的bean对象的类型
lazy-init="true":延迟加载(懒汉模式)
1.bean的方式定义对象
-->
<!-- <beanlazy-init="true"/>-->
<!--2.静态工厂方式创建学生对象-->
<bean id="stu"factory-method="createInstance"/>
<!--3.实例工厂创建对象-->
<bean id="fac"/>
<bean id="stu2" factory-bean="fac" factory-method="createInstance"/>
</beans>
2.bean标签常用属性:
代码整体布局:
运行结果:
添加/修改的代码如下:
spring-2.xml:(添加)
<?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.xsd"
>
<!--
class属性:用于指定要创建的bean对象的类型,必须写的属性
id属性: 用于唯一标识该类型对象,不能重复,但是可以省略
name属性: 可以认为是对bean对象起的别名,可以设置多个名字,用空格或逗号分隔。
现在一般不用
lazy-init:是否是延迟加载(懒汉,饿汉)。可以在beans中配置全局的延迟加载属性default-lazy-init
scope:定义创建对象的方式和范围,singleton(单例),prototype(多例),request,session,global-session
singleton(单例):表示容器中,只有一个对象,每次获取的都是同一个对象
prototype(多例):每次都是new一个新的对象返回
init-method:定义bean生命周期的初始化函数
destroy-method:定义bean生命周期的销毁函数
-->
<bean id="stu"
name="s1 s2"lazy-init="true"
scope="prototype"
init-method="initStu"
destroy-method="destroyStu" />
</beans>
Student:(修改)
package com.entity;
import org.springframework.beans.factory.annotation.Autowired;
public class Student {
public Student(){
System.out.println("这是Student的构造方法...");
}
//自我介绍
public void introduce(){
System.out.println("我是一个学生");
}
//学生初始化
public void initStu(){
System.out.println("调用了学生的初始化方法");
}
//学生销毁
public void destroyStu(){
System.out.println("调用了学生的销毁方法");
}
}
(test)Test2:
package com.test;
import com.entity.Student;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClasspathXmlApplicationContext;
public class Test2 {
public static void main(String[] args) {
//创建spring容器
ApplicationContext ctx = new ClasspathXmlApplicationContext("spring-2.xml");
//根据id获取学生对象
Student s1 =(Student) ctx.getBean("stu");
System.out.println("s1:"+s1);
//根据类型获取学生对象
Student s2 = (Student) ctx.getBean(Student.class);
System.out.println("s2:"+s2);
//根据id和类型获取学生对象
Student s3 = ctx.getBean("stu",Student.class);
System.out.println("s3:"+s3);
//根据name属性获取学生对象
Student s4 = (Student) ctx.getBean("s1");
Student s5 = (Student) ctx.getBean("s2");
System.out.println("s4:"+s4);
System.out.println("s5:"+s5);
}
}
3.单例模式和多例模式的对象:
运行结果:
God:
package com.singleton;
public class God {
public void createWorld(){
System.out.println("上帝创造了世界....");
}
}
Test:
package com.singleton;
public class Test {
public static void main(String[] args) {
God god = new God();
god.createWorld();
System.out.println(god);
God god2 = new God();
god2.createWorld();
System.out.println(god2);
}
}
(singleton)God:(添加)
package com.singleton;
public class God {
//3.定义静态私有对象
private static God god;
//1.构造私有化
private God(){
}
//2.定义静态工厂获取实例的方法,用于获取对象
public static God createInstance(){
if (god==null){
god = new God();
}
return god;
}
public void createWorld(){
System.out.println("上帝创造了世界....");
}
}
(singleton)Test:(添加)
package com.singleton;
public class Test {
public static void main(String[] args) {
God god = God.createInstance();
god.createWorld();
System.out.println(god);
God god2 = God.createInstance();
god2.createWorld();
System.out.println(god2);
}
}
4.beanfactory和ApplicationContext:
// A code block
var foo = 'bar';
// A code block
var foo = 'bar';
// A code block
var foo = 'bar';
// A code block
var foo = 'bar';
// A code block
var foo = 'bar';
// A code block
var foo = 'bar';
// A code block
var foo = 'bar';
// A code block
var foo = 'bar';
// A code block
var foo = 'bar';
关于java.beans.beancontext.BeanContextServices的实例源码和java.beans.introspection的介绍已经告一段落,感谢您的耐心阅读,如果想了解更多关于android.view.textservice.SentenceSuggestionsInfo的实例源码、android.view.textservice.TextServicesManager的实例源码、ApplicationContext注入Bean(多线程中注入Bean)、bean实例化的三种方式 bean标签常用属性 单例模式和多例模式的对象 BeanFactory和ApplicationContext:的相关信息,请在本站寻找。
本文标签: